2015-05-21 71 views
1

我正在使用Asana API,在進行API調用以獲取新的刷新令牌時遇到問題,請讓我知道我錯在哪裏。使用AJAX的Asana API集成

我正在通過Javascript AJAX進行API調用。

app.asana.com/-/oauth_token (POST request)

POST參數

{ 
    "grant_type":"refresh_token", 
    "client_id":"", 
    "client_secret":"", 
    "redirect_uri":"", 
    "refresh_token":"" 
} 

這是我使用的代碼:

$.ajax({ 
    url : app.asana.com/-/oauth_token, 
    dataType : 'jsonp', 
    method: 'POST', 
    beforeSend: function (xhr) { 
     xhr.setRequestHeader("Authorization", "Bearer " + accessToken); 
    }, 
    success : function(result) {}, 
    error: function(jqXHR, textStatus, errorThrown){} 
}); 

我收到返回的錯誤是:

{ 
    "error": "unsupported_grant_type", 
    "error_uri": "asana.com/developers/documentation/getting-started/…;, 
    "error_description": "The supported grant types are authorization_code and refresh_token." 
} 
+0

您應該發佈您正在使用的代碼和您收到的錯誤。 – McWayWeb

+0

這是我使用---> $。阿賈克斯({ \t \t URL代碼:app.asana.com/-/oauth_token, \t \t數據類型: 'JSONP', \t \t方法: 'POST', \t \t beforeSend:function(xhr){ \t \t xhr。setRequestHeader(「Authorization」,「Bearer」+ accessToken); \t \t}, \t \t成功:功能(結果){}, \t \t \t錯誤:功能(jqXHR,textStatus,errorThrown){} \t \t});錯誤是「error」:「unsupported_grant_type」, 「error_uri」:「https://asana.com/developers/documentation/getting-started/authentication」, 「error_description」:「支持的授權類型是authorization_code '和'refresh_token'「。 } –

+0

你如何設置發佈參數?看起來你並沒有發送它們。 –

回答

0

注意對OAuth安全客戶端:

請記住,AJAX調用在客戶端上,並且你正在通話需要你client_secret,你應該當作一個個人密碼。從客戶端進行此調用會向用戶公開,並可能導致安全漏洞。

如果您希望嚴格執行客戶端的Asana的OAuth(例如JavaScript),我建議您使用隱式授權類型。這裏的缺點是你不會獲得刷新令牌,而是在你的access_token過期時需要重新授權你的客戶端。


解決的問題:

撥打電話時獲得新的訪問令牌使用刷新令牌,你需要做的是類似如下的要求:

 //Request 
    POST https://app.asana.com/-/oauth_token 

    //POST Parameters 
    //Must be either form-data or x-www-form-urlencoded 
    client_id=<client_id> 
    client_secret=<client_secret> 
    grant_type="refresh_token" 
    refresh_token=<refresh_token> 

請注意,參數必須爲表格數據或x-www-form-urlencoded


官方體位節點庫

我也建議使用官方NodeJS Asana Library實施任何這一點。

+0

我去了文件並下載了NodeJS Asana Library,裏面沒有找到asana.js。是否有任何文件或鏈接,我可以找到整個信息而不會錯過任何一個步驟。 –

+0

您要麼不正確地從您的Web服務器提供文件,要麼沒有在您的客戶端上使用正確的路徑包含它。我更新了自述文件,包括直接從GitHub發佈中獲取它。如果它解決了提出的問題,請將上述答案標記爲準確。 –