I am trying to implement Google Oauth in our application. I am using google .Net client library "Google.Apis 1.45 version(NuGet package)".
After getting the code from the browser I want to send code_verifier also to get the token. I am using GoogleAuthorizationCodeFlow class "ExchangeCodeForTokenAsync" to get the token. If I am not using "code_challenge", Then I am getting the token successfully. But when I am trying to use "code_challenge" I am getting the below error message.
Error:"invalid_grant", Description:"Missing code verifier.", Uri:"".
Please check my code.
var clientSecrets = new ClientSecrets
{
ClientId = _clientAppDetails.ClientID,
ClientSecret = _clientAppDetails.ClientSecret
};
var credential = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = clientSecrets,
Scopes = new[] { GoogleScope.ImapAndSmtp.Name, GoogleScope.UserInfoEmailScope.Name, GoogleScope.EmailScope.Name }
});
string state = RandomDataBase64url(32);
string code_verifier = RandomDataBase64url(32);
string code_challenge = Base64urlencodeNoPadding(sha256(code_verifier));
string code_challenge_method = "S256";
string redirectURI = string.Format("http://{0}:{1}/", IPAddress.Loopback, GetRandomUnusedPort());
AuthorizationCodeRequestUrl url = credential.CreateAuthorizationCodeRequest(redirectURI);
url.State = state;
string oauthrequestedURL = url.Build().ToString();
oauthrequestedURL += "&code_challenge=" + code_challenge + "&code_challenge_method=" + code_challenge_method;
// Opens request in the browser.
System.Diagnostics.Process.Start(oauthrequestedURL);
/After successful login we are able to get the Code
var context = httpListener.GetContextAsync().Result;
// Sends an HTTP response to the browser.
var response = context.Response;
//Then we are parsing the response and get the success code and pass this code and redirect URI to ExchangeCodeForTokenAsync method.
TokenResponse tokenResponse = credential.ExchangeCodeForTokenAsync("", code, redirectURI, CancellationToken.None).Result;
when we call this above line we are getting the error message
Error:"invalid_grant", Description:"Missing code verifier.", Uri:"".
How can we pass the "grant_type" and "code_verifier" when calling "ExchangeCodeForTokenAsync" method?