Example #1
0
        private async Task <string> GetAuthIdFromCode(string code, string redirectUrl)
        {
            string tokenRequestUrl = $"{tokenEndpoint}?code={code}&redirect_uri={redirectUrl}" +
                                     $"&client_id={clientId}&code_verifier={code_verifier}&client_secret={clientSecret}&scope=&grant_type=authorization_code";

            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"];
                string refreshToken = tokenEndpointDecoded["refresh_token"];
                configHandler.SetYoutubeRefreshToken(refreshToken);

                return(tokenEndpointDecoded["access_token"]);
            }
        }