javascript - 使用 AJAX 集成 Asana API

标签 javascript ajax asana

我正在使用 Asana API,在进行 API 调用以获取新的刷新 token 时遇到问题,请告诉我哪里错了。

我正在通过 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." 
}

最佳答案

OAuth 安全客户端注意事项:

请记住,AJAX 调用是在客户端上进行的,并且您进行的调用需要您的 client_secret,您应将其视为个人密码。从客户端进行此调用会将其暴露给您的用户,并可能导致安全漏洞。

如果您想严格在客户端(例如在 JavaScript 中)为 Asana 实现 OAuth,我建议您使用隐式授权类型。这里的缺点是您不会获得刷新 token ,而是需要在 access_token 过期时重新授权您的客户端。

<小时/>

解决问题:

当使用刷新 token 调用以获取新的访问 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>

请注意,参数必须是 form-data 或 x-www-form-urlencoded

<小时/>

官方 Asana 节点库

我还建议使用官方 NodeJS Asana Library实现其中任何一个。

关于javascript - 使用 AJAX 集成 Asana API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30365439/

相关文章:

javascript - 将多个操作传递给 bindActionCreators

javascript - js - 跟踪容器没有子元素的时刻

php - 为 Asana php 创建 webhook

asana - 工作区 : Not the correct type

javascript - 如何使用 OneDrive REST API 将文件上传到 OneDrive?

javascript - PHP/将数字作为字符串发布,intval无法将其转换为数字

PHP 创建 JSON 数组列表(数据来自 MySQL)

javascript - AJAX 请求从聊天数据库获取最新的、唯一的消息

php - 通过 AJAX 传递希伯来语编码文本 (windows 1255)

asana - 如何从我的工作区中删除 asana 中的用户?