Search code examples
c#google-apigoogle-oauth

Issue Exchanging Authorization Code for Token Google Api


I have checked every thing repeatedly and does not seem to have any issue in code or logic.

Here is the code:

var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
    {"grant_type", "authorization_code"},
    {"code", code},
    {"redirect_uri", redirectUri.ToString()},
    {"client_id", clientId},
    {"client_secret", clientSecret}
});
    string token = string.Empty;


    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
        var response = client.PostAsync(tokenEndpoint, content);
        var responseContent = response.Result.Content.ReadAsStringAsync();
        token = responseContent.ToString();
        if (!response.Result.IsSuccessStatusCode) ;
    }

    return token;

And i am getting error:

"error": "redirect_uri_mismatch",
"error_description": "Bad Request"

The Url of the above page is : http://localhost:62188/dashboard/googleSync.aspx

And in Google Console i have this Url as Authorized Return Url :

enter image description here

So what am i missing here?


Solution

  • Ok: had to use this : :redirect_uri = "postmessage" and it worked.