Google API令牌端点POST返回错误请求400 [英] Google API token endpoint POST returns Bad Request 400

查看:433
本文介绍了Google API令牌端点POST返回错误请求400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为访问令牌交换一次性Google Plus授权代码。但我一直在收到400错误请求。我正在使用VB.NET。这里是代码:

 '我们现在应该有一个存储在code$ b中的好一次性授权代码$ b使用Client作为新的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,值)
responseString = Encoding.Default.GetString(Resp)
End使用

我很确定这是我应该使用的端点< a href =https://www.googleapis.com/oauth2/v3/token =nofollow noreferrer> https://www.googleapis.com/oauth2/v3/token 但是谁知道? Google发现文档对我来说简直就是这样。



还请原谅我的天真,但是有人会解释一下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发送的错误?

所以答案如下:该API需要2个电话。第一次调用返回一次性授权码。第一次调用必须指定一个重定向URI。



第二次调用将一次性代码发送给授权令牌的API。此POST还需要一个重定向URI。



第一次调用中的重定向URI必须与第二次调用中的重定向URI相同!!!

strong>



我无法在文档的任何位置找到它。请记住,此URI还必须与开发者控制台中列表中的URI之一相匹配,这就是所有文档所说的。


I am trying to exchange a one-time Google Plus Authorization code for an access token. But I keep on getting a 400 Bad Request. I am using VB.NET. Here is the code:

        '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

I'm pretty sure this is the endpoint I'm supposed to be using https://www.googleapis.com/oauth2/v3/token but who knows? The Google Discovery Document just muddles this for me.

Also pardon my naivete but would someone explain how the POST code Google uses as an example relates to the Web Request in my code above? I think I understand how the values translate, but the 3 header lines in the POST (below) ... how does this get specified in the VB code? I'm missing something that must be really obvious to others so if you know, please tell me.

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

Another Stack Overflow Post says something about sending the data as query parameters (using '&' I guess) instead of sending the data as request headers, so is there something wrong with sending along a NameValueCollection?

解决方案

So here's the answer: The API requires 2 calls. The first call returns a one time authorization code. The first call must specify a redirect URI.

The 2nd call sends the one time code to the API for an authorization token. This POST also requires a redirect URI.

The redirect URI in the first call must be the same as the redirect URI in the 2nd call!!!

I was unable to find this anywhere in the documentation. Remember that this URI must also match one of the URIs in the list in your developers console, which is what all the documentation says.

这篇关于Google API令牌端点POST返回错误请求400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆