我的目标是获得一个Chrome扩展程序,以便能够将联系人添加到Google联系人中。这些文件是:
{
"manifest_version": 2,
"name": "My friend joe",
"version": "1.0",
"description": "Add Joe to google contacts",
"browser_action": {},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"identity"
],
"oauth2": {
"client_id": "1038191206887-v1987tg5v07mp166l0pm68qqblojvpll.apps.googleusercontent.com",
"scopes": ["https://www.google.com/m8/feeds/"]
}
}
chrome.browserAction.onClicked.addListener(add_joe);
function add_joe() {
chrome.identity.getAuthToken({"interactive": true}, add_contact);
return true;
}
function add_contact(t) {
console.log('Token: ' + t);
// Generate the body of the XMLHttpRequest
xhr_body =
`<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:gd="http://schemas.google.com/g/2005">
<atom:category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:fullName>Joe Schmoe</gd:fullName>
</gd:name>
</atom:entry>`;
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://www.google.com/m8/feeds/contacts/default/full");
xhr.setRequestHeader("Content-Type", "application/atom+xml");
xhr.setRequestHeader("GData-Version", "3.0");
xhr.setRequestHeader("Authorization", "Bearer " + t);
xhr.send(xhr_body);
}
错误发生之前采取的步骤:
将API密钥添加到清单。然后我使用开发人员模式将解压缩的扩展程序加载到chrome中
这就是发生错误的时间,如上面的屏幕快照所示。通过单击Inspect views: background page
扩展页面并查看控制台,可以看到这一点。
参考资源:
I'm using the Creating contacts
section of the Google Contacts API to determine the request headers and request body. Also it says "To create a new contact, send an authorized POST request".
How to send an authorized request is demonstrated in their javascript API client library docs.
I know the problem stems from issues making a CORS request (Cross-Origin-Resource Sharing). That information is also in the javascript API client library docs but the only example code is using their client library function calls.
在输入问题的过程中,我遇到了答案。我找到的特定文档是:https : //developer.chrome.com/extensions/xhr。只需添加任何域,我将向提出CORS请求,并在列表中预先列出manifest.json
。因此,在修改我的清单以使其包括:
"permissions": [
"identity",
"https://www.google.com/"
],
我不再收到该错误,并且实际上收到了201 Created
状态码响应。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句