2014-12-03 30 views
0

我試圖在Google Apps腳本中使用StackExchange API的V2.2。Google Apps腳本中的StackExchange API身份驗證

當我嘗試發送POST請求以獲取訪問令牌時,問題出現在顯式OAuth 2.0流程的最後一步。我收到一個404錯誤,但手動發送相同的請求(使用郵遞員擴展)一切正常。

爲了簡化問題,如果我送,沒有有效載荷這個POST請求我收到相同的404

var response = UrlFetchApp.fetch("https://stackexchange.com/oauth/access_token", { 
    method: 'post', 
    muteHttpExceptions: true 
}); 
Logger.log(response); 

而郵遞員我收到此400:

{ 
    "error": { 
     "type": "invalid_request", 
     "message": "client_id not provided" 
    } 
} 

我想這將是UrlFetchApp存在問題,但有誰知道如何解決它?謝謝!

回答

0

該問題與Origin頭相關。

不能從頭部直接刪除,但你可以通過代理:)

0

您需要通過向呼叫添加「選項」對象來提供帖子的數據。 例如:

var options = { "method" : "post", "payload" : payload }; 
UrlFetchApp.fetch("https://stackexchange.com/oauth/access_token", options); 

順便說一句,你有沒有嘗試過,你使用網址抓取得到的OAuth:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#addOAuthService(String) - 這可能是更好的辦法。

+0

由於進行通話,但我得到了同樣的錯誤。我不使用OAuthConfig,因爲它不適用於使用OAuth2的API。我使用[此庫](https://github.com/googlesamples/apps-script-oauth2)的改編,但在此步驟中失敗。 – Gerry 2014-12-03 12:10:20