[英]Chrome extension gmail API cookiePolicy?
我正在構建一個chrome擴展程序,它將讀取用戶的電子郵件並檢查它們是否存在拼寫錯誤。 但是,當我嘗試在我的background.js中驗證用戶時,我遇到了這個錯誤:
uO {message:“無效的cookiePolicy”,堆棧:“gapi.auth2.ExternallyVisibleError:無效的cookieP ...在handleResponse(extensions :: sendRequest:67:7)”}
這是我試圖驗證它們的方式:
background.js
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://apis.google.com/js/client.js?onload=callbackFunction";
head.appendChild(script);
chrome.identity.getAuthToken({interactive: true}, authorize);
function authorize(token) {
gapi.auth.authorize({
client_id: '800382879116-k3luktdc1lmb1e1fml8i8u.apps.googleusercontent.com',
immediate: true,
scope: 'https://www.googleapis.com/auth/gmail.readonly'
},
function(result){
console.log(result);
gapi.client.load('gmail', 'v1', callback);
});
}
background.html
<!DOCTYPE html>
<html>
<body>
<script src='scripts/background.js'></script>
</body>
</html>
的manifest.json
{
"name": "Gmail Typo Analyzer",
"version": "0.1",
"description": "Gmail Typo Analyzer",
"permissions": [
"identity",
"storage"
],
"content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'",
"oauth2": {
"client_id": "82879116-k3luktdc1li8u.apps.googleusercontent.com",
"scopes": [
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/userinfo.email"
]
},
"browser_action": {
"default_popup": "popup.html",
"default_icon": "images/Icon_16.png"
},
"background": {
"page": "background.html",
"persistent": false
},
"icons": {
"16": "images/Icon_16.png",
"32": "images/Icon_32.png",
"48": "images/Icon_48.png",
"128": "images/Icon_128.png"
},
"manifest_version": 2,
"key": "c0Kn5f+t92r4P8lmmoDlKtQ6X9Q42UfFtkkiSRBAVMPHnIHqOQvYC67VczJefSNTGpUYa8+wQDFoFj/clH9SfR+BvOGgI6BUVKBNGGoFS"
}
我現在非常失落,因為他們似乎並不是實現我在任何地方所做的事情的明確指南。 有誰知道我可能做錯了什么?
您沒有發布您的manifest.json
文件,您將在其中設置oauth2
憑據 ,因此我會嘗試以下方法:
的manifest.json
...
"oauth2" : "client_id": "800382879116-k3luktdc1lmb1e1fml8i8u.apps.googleusercontent.com",
"scopes": [
"https://www.googleapis.com/auth/gmail.readonly"
]
...
background.js
chrome.identity.getAuthToken({interactive: true}, authorize);
function authorize(token) {
if (token) {
//user has given authorization, use token for requests.
//...
} else {
//no authorization received.
console.log('No authorization. Error: ' + chrome.runtime.lastError);
}
};
而且您無需加載Google API客戶端,您可以使用XMLHttpRequest
或Fetch API訪問Gmail的Restful API 。
我過去常常遇到Cookie錯誤,但對於Chrome擴展程序,我只知道如何在Extensions選項卡中解壓縮它們。 所以使用gapi直接對我沒用。
正如Ivan所說,Chrome擴展程序通過在清單中設置“oauth2”部分來“內置”這種支持。 然后,您可以像Ajan一樣直接使用Ajax調用API。
以Ivan為例,這是我的工作代碼,用於獲取聯系人列表。 我還沒有從XHR對象中讀取,但Fiddler確認數據恢復正常,沒有任何cookie或CORS錯誤。 當然,請確保在console.developers.google.com中啟用這些API。
chrome.identity.getAuthToken({ interactive: true }, authorize);
function authorize(token) {
if (token) {
console.log(token);
var xhr = new XMLHttpRequest();
xhr.open('GET',
"https://people.googleapis.com/v1/people/me/connections?personFields=names");
xhr.setRequestHeader('Authorization',
'Bearer ' + token);
xhr.send();
//user has given authorization, use token for requests.
//...
} else {
console.log('No authorization. Error: ' + chrome.runtime.lastError);
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.