2015-02-08 96 views
1

我試圖通過傳遞下面屏幕截圖中顯示的參數來獲取使用REST API的訪問令牌,但我面臨錯誤Required parameter is missing: grant_type。我已經在Chrome瀏覽器中使用cURL和Advanced Rest API進行了嘗試。無法獲取Google OAuth 2.0訪問令牌

client_secret;xxx 
refresh_token; 
redirect_uri;https%3A%2F%2Fwww%2Eexample%2Ecom%2Foauth2callback 
scope;https://www.googleapis.com/auth/drive 
client_id;xxxxxxxx.apps.googleusercontent.com 
access_token; 

捲曲語法:

D:\>curl -k --header Content-Type=application/x-www-form-urlencoded -i -X POST "https://accounts.google.com/o/oauth2/token" -F 'code=4%2FdpbtMVUpzNFAfGHlxrNvfMlvHrPWk_m8y2I-thDdsLk.wvBSFgWEqW0Zcp7tdiljKKZFG-jOlgI' -F 'client_id=xxxxxxxx.apps.googleusercontent.com' -F 'client_secret=xxx' -F 'redirect_uri=https%3A%2F%2Fwww%2Eexample%2Ecom%2Foauth2callback' -F 'grant_type=authorization_code' 

如何解決這個問題,我使用捲曲和先進的REST API發送這些參數?

回答

0

顯然你調用令牌端點來交換code作爲access_token作爲授權代碼授權的一部分。有幾件事情:

  1. 不使用-F標誌,因爲它會產生一個multipart-formdata頭,但你需要www-form-urlencoded-d生產
  2. 不使用-k標誌向谷歌爲使事情變得不安全;谷歌介紹,你應該檢查
  3. 有效的SSL證書放在引號

Content-Type參數添加一些額外的語法優化後,你應該能夠使用:

curl -v -i \ 
    -d "code=4%2FdpbtMVUpzNFAfGHlxrNvfMlvHrPWk_m8y2I-thDdsLk.wvBSFgWEqW0Zcp7tdiljKKZFG-jOlgI" \ 
    -d 'client_id=xxxxxxxx.apps.googleusercontent.com' \ 
    -d 'client_secret=xxx' \ 
    -d 'redirect_uri=https%3A%2F%2Fwww%2Eexample%2Ecom%2Foauth2callback' \ 
    -d 'grant_type=authorization_code' \ 
    https://accounts.google.com/o/oauth2/token 
相關問題