我正在与google auth合作,从Windows Store应用程序访问GMail。我获得了成功的授权代码,现在我正试图将其交换为访问代码。我发送POST查询,因为它描述在谷歌Gide安装的应用程序:https://developers.google.com/accounts/docs/OAuth2InstalledApp。我的POST查询被feedler捕获:
POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: accounts.google.com
Content-Length: 193
Expect: 100-continue
Connection: Keep-Alive
code=4/_ynXWYzGSols1p7lzuVBc59iGhgo& client_id={my client_id obtained during application registration} & client_secret={ my client secret obtained during application registration encoded by Uri.EscapeDataString()}& redirect_uri={ urn:ietf:wg:oauth:2.0:oobencoded by Uri.EscapeDataString()} & grant_type=authorization_code
服务器返回"(400)错误请求"。我的请求有什么问题?
不要在值上调用Uri.EscapeDataString()
。如果您查看链接到的页面上的示例,它们也没有转义。
下面是我使用的代码片段:
var form = new Dictionary<string, string>
{
{"code", authenticationCode},
{"client_id", Keys.ClientId},
{"client_secret", Keys.ClientSecret},
{"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"},
{"grant_type", "authorization_code"},
};
var content = new FormUrlEncodedContent(form);
var client = new HttpClient();
var response = await client.PostAsync("https://accounts.google.com/o/oauth2/token", content);
var json = await response.Content.ReadAsStringAsync();