Beispiel #1
0
        private async Task <string> RefreshAccessToken()
        {
            if (!configHandler.HasYoutubeRefreshToken)
            {
                return("");
            }

            string refreshToken    = configHandler.Config.YoutubeRefreshToken;
            string tokenRequestUrl = $"{tokenEndpoint}?refresh_token={refreshToken}&client_id={clientId}&client_secret={clientSecret}&grant_type=refresh_token";

            HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create(tokenRequestUrl);

            tokenRequest.Method      = "POST";
            tokenRequest.ContentType = "application/x-www-form-urlencoded";
            tokenRequest.Accept      = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            byte[] byteVersion = Encoding.ASCII.GetBytes(tokenRequestUrl);
            tokenRequest.ContentLength = byteVersion.Length;
            using (System.IO.Stream stream = tokenRequest.GetRequestStream())
            {
                await stream.WriteAsync(byteVersion, 0, byteVersion.Length);
            }

            WebResponse tokenResponse = await tokenRequest.GetResponseAsync();

            using (StreamReader reader = new StreamReader(tokenResponse.GetResponseStream()))
            {
                string responseText = await reader.ReadToEndAsync();

                Dictionary <string, string> tokenEndpointDecoded = JsonConvert.DeserializeObject <Dictionary <string, string> >(responseText);

                string accessToken = tokenEndpointDecoded["access_token"];
                configHandler.SetYoutubeAccessToken(accessToken);
                return(accessToken);
            }
        }