2012-08-04 74 views
4

我知道這已被問過,但在其他職位的建議並沒有幫助。 我成功收到來自google的授權令牌,但是當我嘗試獲取訪問令牌時,我收到了一個invalid_request。谷歌API - 從Oauth2請求令牌返回「invalid_request」

我們在PHP中的第一次嘗試導致了這種反應,當我通過HTTP客戶端嘗試時,我得到了同樣的結果。最後,我用CURL直接試過,仍然得到了相同的結果:

curl -v -k --header "Content-Type: application/x-www-form-urlencoded" \ 
-F 'code=4/U2su0fwiynq4v8J8xD56ohfF0fX6.EhP0brx3cosbuJJVnL49Cc8EFE7ncQI' \ 
-F 'client_id=-abcdefghijklmnopqrstuvwxyz.apps.googleusercontent.com' \ 
-F 'client_secret=ABC123ABC123ABC123ABC123' \ 
-F 'grant_type=authorization_code' \ 
-F 'redirect_uri=https://app.myserver.com/googleLogin.php' \ 
https://accounts.google.com/o/oauth2/token 

我得到的迴應是:

> https://accounts.google.com/o/oauth2/token 
* About to connect() to accounts.google.com port 443 (#0) 
* Trying 173.194.77.84... connected 
* Connected to accounts.google.com (173.194.77.84) port 443 (#0) 
* SSLv3, TLS handshake, Client hello (1): 
* SSLv3, TLS handshake, Server hello (2): 
* SSLv3, TLS handshake, CERT (11): 
* SSLv3, TLS handshake, Server finished (14): 
* SSLv3, TLS handshake, Client key exchange (16): 
* SSLv3, TLS change cipher, Client hello (1): 
* SSLv3, TLS handshake, Finished (20): 
* SSLv3, TLS change cipher, Client hello (1): 
* SSLv3, TLS handshake, Finished (20): 
* SSL connection using RC4-SHA 
* Server certificate: 
*  subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=accounts.google.com 
*  start date: 2011-07-21 00:00:00 GMT 
*  expire date: 2013-07-18 23:59:59 GMT 
*  common name: accounts.google.com (matched) 
*  issuer: C=ZA; O=Thawte Consulting (Pty) Ltd.; CN=Thawte SGC CA 
*  SSL certificate verify ok. 
> POST /o/oauth2/token HTTP/1.1 
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 
> Host: accounts.google.com 
> Accept: */* 
> Content-Length: 771 
> Expect: 100-continue 
> Content-Type: application/x-www-form-urlencoded; boundary=----------------------------0d3dc3507a7f 
> 
* Done waiting for 100-continue 
< HTTP/1.1 400 Bad Request 
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
< Pragma: no-cache 
< Expires: Fri, 01 Jan 1990 00:00:00 GMT 
< Date: Sat, 04 Aug 2012 22:07:07 GMT 
< Content-Type: application/json 
< X-Content-Type-Options: nosniff 
< X-Frame-Options: SAMEORIGIN 
< X-XSS-Protection: 1; mode=block 
< Server: GSE 
< Transfer-Encoding: chunked 
< 
{ 
    "error" : "invalid_request" 
* Connection #0 to host accounts.google.com left intact 
* Closing connection #0 
* SSLv3, TLS alert, Client hello (1): 

我不是URL編碼任何東西之前我送它,但是當我做,我得到相同的結果。

任何幫助,非常感謝!

回答

5

嘗試使用OAuth 2.0遊樂場執行相同的請求,並將它們與你的比較:

https://code.google.com/oauthplayground/

+1

感謝克勞迪奧 - 但我看不出有什麼差別。該應用程序確認參數必須是網址編碼的(如我所想),並且我要發送的有效負載中的所有內容*都是*編碼的。我看到的唯一區別是,我的client_id比Google更長,格式也不同,我看到的其他許多示例(Google的:407408718192.apps.googleusercontent.com) – rhuff 2012-08-05 04:19:37

+0

知道了!在我對授權代碼的請求中,我的'approval_prompt'被設置爲'auto' - 谷歌將他們設置爲'force'。我改變了我的武力,它的工作。 感謝您的正確方向的提示! – rhuff 2012-08-05 04:39:00

+0

這並沒有幫助我,我仍然有同樣的錯誤。無效的請求。 – Michael 2013-12-15 16:36:59

相關問題