Beispiel #1
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);
        }
Beispiel #2
0
        public IList <IYoutubePlaylist> LoadPlaylists(IYoutubeAccount account)
        {
            LOGGER.Info($"Loading playlists of account with id: '{account.Id}' and title: '{account.Title}' from youtube");

            var list = new List <IYoutubePlaylist>();

            string nextPageToken = null;

            do
            {
                string         pagetoken = !string.IsNullOrWhiteSpace(nextPageToken) ? $"&pageToken={nextPageToken}" : string.Empty;
                string         url       = $"https://youtube.googleapis.com/youtube/v3/playlists?part=snippet&maxResults=50&mine=true{pagetoken}&key={YoutubeClientData.YoutubeApiKey}";
                HttpWebRequest request   = (HttpWebRequest)WebRequest.Create(url);
                request.Method          = "GET";
                request.Credentials     = CredentialCache.DefaultCredentials;
                request.ProtocolVersion = HttpVersion.Version11;
                request.Accept          = "application/json";

                // Header schreiben
                request.Headers.Add($"Authorization: Bearer {account.Access.GetActiveToken()}");

                var result = WebService.Communicate(request);
                QuotaProblemHandler.ThrowOnQuotaLimitReached(result);

                Response response = JsonConvert.DeserializeObject <Response>(result);

                nextPageToken = response.nextPageToken;
                foreach (var serializedPlaylist in response.items)
                {
                    list.Add(new YoutubePlaylist()
                    {
                        Title       = serializedPlaylist.snippet.title,
                        Id          = serializedPlaylist.id,
                        PublishedAt = serializedPlaylist.snippet.publishedAt
                    });
                }
            } while (!string.IsNullOrWhiteSpace(nextPageToken));

            foreach (var playlist in list)
            {
                LOGGER.Info($"Loaded playlist with id: {playlist.Id} and title: '{playlist.Title}'. Playlist has been published at: '{playlist.PublishedAt}'");
            }

            return(list);
        }
Beispiel #3
0
        private static Response GetAccountDetails(IYoutubeAccountAccess access)
        {
            string         url     = $"https://www.googleapis.com/youtube/v3/channels?part=snippet&mine=true&key={YoutubeClientData.YoutubeApiKey}";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method          = "GET";
            request.Credentials     = CredentialCache.DefaultCredentials;
            request.ProtocolVersion = HttpVersion.Version11;

            // Header schreiben
            request.Headers.Add($"Authorization: Bearer {access.AccessToken}");

            var result = WebService.Communicate(request);

            QuotaProblemHandler.ThrowOnQuotaLimitReached(result);

            Response response = JsonConvert.DeserializeObject <Response>(result);

            return(response);
        }
        public static ICategory[] GetVideoCategories(string regionCode, string accessToken)
        {
            LOGGER.Info($"Loading video categories from youtube");

            var            pageToken = string.Empty;
            CultureInfo    ci        = CultureInfo.CurrentUICulture;
            string         url       = string.Format("https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&hl={2}&regionCode={1}&key={0}", YoutubeClientData.YoutubeApiKey, regionCode, ci.Name);
            HttpWebRequest request   = (HttpWebRequest)WebRequest.Create(url);

            request.Proxy           = null;
            request.Method          = "GET";
            request.Credentials     = CredentialCache.DefaultCredentials;
            request.ProtocolVersion = HttpVersion.Version11;

            // Header schreiben
            request.Headers.Add(string.Format("Authorization: Bearer {0}", accessToken));

            var result = WebService.Communicate(request);

            QuotaProblemHandler.ThrowOnQuotaLimitReached(result);

            Response response = JsonConvert.DeserializeObject <Response>(result);

            if (response.items == null)
            {
                response.items = new Item[0];
                LOGGER.Error($"Could not load video categories from youtube!");
            }

            var categories = response.items.Where(i => i.snippet.assignable).Select(i => new YoutubeCategory(int.Parse(i.id), i.snippet.title)).ToArray();

            foreach (var cat in categories)
            {
                LOGGER.Info($"Adding category with id: {cat.Id} and title: {cat.Title}");
            }

            return(categories);
        }
Beispiel #5
0
        public static ILanguage[] GetLanguages(string accessToken)
        {
            LOGGER.Info($"Loading languages from youtube");

            var            pageToken = string.Empty;
            CultureInfo    ci        = CultureInfo.CurrentUICulture;
            string         url       = string.Format("https://www.googleapis.com/youtube/v3/i18nLanguages?part=snippet&hl={1}&key={0}", YoutubeClientData.YoutubeApiKey, ci.Name);
            HttpWebRequest request   = (HttpWebRequest)WebRequest.Create(url);

            request.Proxy           = null;
            request.Method          = "GET";
            request.Credentials     = CredentialCache.DefaultCredentials;
            request.ProtocolVersion = HttpVersion.Version11;

            var result = WebService.Communicate(request);

            QuotaProblemHandler.ThrowOnQuotaLimitReached(result);

            Response response = JsonConvert.DeserializeObject <Response>(result);

            if (response.items == null)
            {
                response.items = new Item[0];
                LOGGER.Error($"Could not load languages from youtube!");
            }

            var languages = response.items.Select(i => new YoutubeLanguage()
            {
                Id = i.id, Hl = i.snippet.hl, Name = i.snippet.name
            }).OrderBy(lang => lang.Name).ToArray();

            foreach (var lang in languages)
            {
                LOGGER.Info($"Adding language with id: {lang.Id}, hl: {lang.Hl} and title: '{lang.Name}'");
            }

            return(languages);
        }