2

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?

3
  • Please edit your question and include your code. Why do you want to do anything why not just let the library handle it? Commented Apr 14, 2020 at 12:27
  • Hi DakmTo, I am not able to find the properties to pass the Code_Verifier to the API. Without "code_challenge" API is working fine and I am able to get the token successfully. Commented Apr 15, 2020 at 7:12
  • What api exactly are you trying to connect to. It almost looks like are you trying to use The google .net client library to connect to Xoauth for the imap server? The latter being far out of scope for this library it is intended to connect to the Google discovery services apis. Commented Apr 15, 2020 at 8:58

1 Answer 1

0

You can try adding your extra parameters using UserDefinedQueryParams, maybe that will work.

var credential = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    {
        ClientSecrets = clientSecrets,
        Scopes = new[] { GoogleScope.ImapAndSmtp.Name, GoogleScope.UserInfoEmailScope.Name, GoogleScope.EmailScope.Name },
        UserDefinedQueryParams = new KeyValuePair<string, string>[2] { 
            new KeyValuePair("code_challenge", code_challenge), 
            new KeyValuePair("code_challenge_method", code_challenge_method)  
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.