Google API令牌端点POST返回错误请求400

Ste*_*erg 6 authentication api authorization google-api bad-request

我正在尝试为访问令牌交换一次性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
Run Code Online (Sandbox Code Playgroud)

我很确定这是我应该使用的端点https://www.googleapis.com/oauth2/v3/token但谁知道呢?在谷歌发现文档只是muddles这对我来说.

同时请原谅我的天真,但有人会解释一下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
Run Code Online (Sandbox Code Playgroud)

另一个Stack Overflow Post说了一些关于将数据作为查询参数发送的信息(使用'&'我猜),而不是将数据作为请求头发送,所以发送NameValueCollection有什么问题吗?

Ste*_*erg 5

所以答案就是:API需要2个电话.第一个调用返回一次授权代码.第一个调用必须指定重定向URI.

第二个调用将一次性代码发送到API以获取授权令牌.此POST还需要重定向URI.

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

我无法在文档中的任何地方找到它.请记住,此URI还必须与开发人员控制台列表中的某个URI匹配,这就是所有文档所说的内容.