簡體   English   中英

Google API令牌端點POST返回錯誤請求400

[英]Google API token endpoint POST returns Bad Request 400

我正在嘗試為訪問令牌交換一次性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文檔對我來說只是混淆了這一點。

同時請原諒我的天真,但有人會解釋一下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相同!!!

我無法在文檔中的任何地方找到它。 請記住,此URI還必須與開發人員控制台列表中的某個URI匹配,這就是所有文檔所說的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM