【发布时间】:2015-03-01 19:07:50
【问题描述】:
我正在尝试用一次性 Google Plus 授权码换取访问令牌。但我不断收到 400 Bad Request。我正在使用 VB.NET。代码如下:
'We should now have a "good" one-time authorization code stored in "code"
Using Client As New WebClient()
'Dim Client As New WebClient()
Dim values As New NameValueCollection()
Dim Resp
Dim responseString As String
values("code") = Request.QueryString("code")
values("client_id") = ConfigurationManager.AppSettings("google.clientid")
values("client_secret") = ConfigurationManager.AppSettings("google.clientsecret")
values("grant_type") = "authorization_code"
values("redirect_uri") = "http://localhost:3333/MyVacations/default.aspx"
Resp = Client.UploadValues("https://www.googleapis.com/oauth2/v3/token", values)
responseString = Encoding.Default.GetString(Resp)
End Using
我很确定这是我应该使用的端点 https://www.googleapis.com/oauth2/v3/token 但谁知道呢? Google Discovery Document 只是让我感到困惑。
还请原谅我的天真,但有人能解释一下 Google 用作示例的 POST 代码与我上面代码中的 Web 请求有何关系吗?我想我理解这些值是如何转换的,但是 POST 中的 3 个标题行(如下)......这是如何在 VB 代码中指定的?我遗漏了一些对其他人来说必须非常明显的东西,所以如果你知道,请告诉我。
POST /oauth2/v3/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
另一个Stack Overflow Post 提到了将数据作为查询参数发送(我猜使用'&')而不是作为请求标头发送数据,那么沿 NameValueCollection 发送有什么问题吗?
【问题讨论】:
-
您的redirect_uri 是否与用于获取授权码的值匹配?
-
根据 POST 返回的响应,我得到一个 uri_redirect_mismatch,所以你是对的。但是我已经将 URL 直接从开发者控制台复制并粘贴到我的代码中,反之亦然,所以我不知道为什么会发生这种情况。
标签: api authentication google-api authorization bad-request