예제 #1
0
        private void RecreateSaved()
        {
            LOGGER.Debug($"Recreating cache of saved accounts");
            Saved = new YoutubeAccountContainer();
            foreach (var account in Container.RegisteredAccounts)
            {
                LOGGER.Debug($"Recreating cache for account '{account.Title}'");
                var newAccount = YoutubeAccount.Create(account.Id, account.Title, account.Region);

                Saved.RegisterAccount(newAccount);
            }
        }
예제 #2
0
        public IYoutubeAccount ConnectToAccount(string code, bool mailsAllowed, IYoutubeClient client, YoutubeRedirectUri redirectUri)
        {
            LOGGER.Info($"Connecting to account, mails allowed: {mailsAllowed}, redirect uri: {redirectUri}");

            var    uri     = redirectUri.GetAttribute <EnumMemberAttribute>().Value;
            string content = $"code={code}&client_id={client.Id}&client_secret={client.Secret}&redirect_uri={uri}&grant_type=authorization_code";
            var    bytes   = Encoding.UTF8.GetBytes(content);

            // Request erstellen
            WebRequest request = WebRequest.Create("https://www.googleapis.com/oauth2/v4/token");

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            string result = WebService.Communicate(request, bytes);

            QuotaProblemHandler.ThrowOnQuotaLimitReached(result);

            IYoutubeAccount account      = null;
            var             authResponse = JsonConvert.DeserializeObject <YoutubeAuthResponse>(result);

            IYoutubeAccountAccess access = new YoutubeAccountAccess();

            access.Client = client;
            access.HasSendMailPrivilegue = mailsAllowed;

            if (authResponse != null && !string.IsNullOrWhiteSpace(authResponse.access_token))
            {
                access.AccessToken    = authResponse.access_token;
                access.RefreshToken   = authResponse.refresh_token;
                access.TokenType      = authResponse.token_type;
                access.ExpirationDate = DateTime.Now.AddSeconds(authResponse.expires_in);
                access.ClientId       = client.Id;

                LOGGER.Info($"Connection successful, loading account details");

                var accountDetails = GetAccountDetails(access);
                var acc            = accountDetails.items.First();
                account = YoutubeAccount.Create(acc.id, acc.snippet.country, acc.snippet.title);

                account.Access.Add(access);
            }

            LOGGER.Info($"Connected to account with id: {account.Id} and title: '{account.Title}'");

            return(account);
        }