发行令牌Google Api的交换授权码

时间:2018-09-12 01:49:46

标签: c# google-api google-oauth

我已经反复检查了每件事,似乎在代码或逻辑上没有任何问题。

代码如下:

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;

我得到了错误:

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

上一页的网址为:http://localhost:62188/dashboard/googleSync.aspx

在Google控制台中,我将此URL作为授权返回URL:

enter image description here

那我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

好:必须使用::redirect_uri =“ postmessage”,它才能起作用。