我正在尝试构建一个Chrome扩展程序,该扩展程序在单击工具栏图标时将调用与Asana的api(https://asana.com/developers/documentation/getting-started/quick-start)接口的函数。
这是我的代码:
清单
{
"manifest_version": 2,
"name": "test",
"description": "asana test",
"version": "1.0",
"browser_action":
{
"default_icon": "icon.png"
},
"permissions": [
"https://app.asana.com/api/**",
"activeTab"
],
"background": {
"scripts": [ "scripts/require.js", "scripts/background.js"],
"persistent": true
}
}
后台脚本
require(['asana'], function (asa) {
var asana = asa;
});
chrome.browserAction.onClicked.addListener(function(tab) {
// replace with your personal access token.
var personalAccessToken = '0/123456789....';
// Construct an Asana client
var client = asana.Client.create().useAccessToken(personalAccessToken);
// Get your user info
client.users.me()
.then(function(me) {
// Print out your information
console.log('Hello world! ' + 'My name is ' + me.name + ' and my primary Asana workspace is ' + me.workspaces[0].name + '.');
});
});
有什么想法我想念的吗?
很抱歉,这个问题太简单了。我遍历了其他类似的线程,但仍然卡住了:(
答案 0 :(得分:0)
通过使用@ajax直接调用api(而不要求asana)解决了此问题。
示例在后台脚本中添加了以下功能
function authTest()
{
$.ajax({
url: 'https://app.asana.com/api/1.0/users/me',
type: 'get',
processData: false,
contentType: 'application/octet-stream',
headers: {
"Authorization": "Bearer 0/....,
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.error(data);
}
})
}