我正在开发Chrome扩展程序以将数据写入电子表格。这是我的第一个扩展。但是,在向电子表格发送请求时,会出现一些奇怪的错误。
错误:cb = gapi.loaded_0:76未捕获的邮件{消息:“无效的cookiePolicy”,堆栈:“ gapi.auth2.ExternallyVisibleError:无效的cookieP…is.google.com / js / client.js?_ = 1537034052136: 3:672)“}
我正在尝试对用户进行身份验证,可能是在某处出错。谁能帮忙吗?
background.js
var CLIENT_ID = "1052562111231-8v0luivnri8vuh6bsmo6v5ocls4apv4m.apps.googleusercontent.com";
var SCOPES = [
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/spreadsheets',
'https://docs.google.com/feeds'
];
var discoveryUrl = 'https://sheets.googleapis.com/$discovery/rest?version=v4';
var x;
xhrWithAuth();
function xhrWithAuth() {
console.log("xhrWithAuth");
var access_token;
var retry = true;
getToken();
function getToken() {
chrome.identity.getAuthToken({
'interactive': true
}, function(token) {
if (chrome.runtime.lastError || !token) {
console.log('getAuthToken', chrome.runtime.lastError.message);
// callback(chrome.runtime.lastError);
return;
}
console.log(token);
$.getScript("https://apis.google.com/js/client.js", function () {
gapi.load('client', function () {
console.log("gapi.client is loaded")
gapi.auth.setToken({
'access_token': token
});
gapi.auth.authorize({
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult(token));
});
});
});
}
}
function handleAuthResult(authResult) {
console.log(authResult)
if (authResult && !authResult.error) {
loadSheetsApi();
} else {
console.log("no authentication");
}
}
function loadSheetsApi() {
console.log('loadSheetsApi');
var requests = ["abc","def"];
console.log(requests);
var discoveryUrl = 'https://sheets.googleapis.com/$discovery/rest?version=v4';
gapi.client.load(discoveryUrl).then(function() {
console.log(gapi.client);
var res = gapi.client.sheets.spreadsheets.values.batchUpdate({
spreadsheetId: '1_dAJ8QqxFjKD1Z32vKe_ERh6AFYlALn0gtGIIifowC8',
requests: requests
});
res.then(function(data) {
console.log(data)
});
});
//writeSheet();
}
manifest.js
{
"name": "Daca",
"version": "1.0",
"manifest_version": 2,
"description": "Update spreadsheet on user Submit",
"browser_action": {
},
"background": {
"scripts": ["background.js","jquery.js"],
"persistent": true
},
"permissions": ["tabs", "<my url>", "https://apis.google.com/js/client.js", "https://accounts.google.com/o/oauth2/token", "identity", "https://content-sheets.googleapis.com/v4/spreadsheets/*"],
"content_scripts": [
{
"matches": ["<my url>"],
"js": ["contentscript.js"],
"run_at": "document_end"
}
],
"oauth2": {
"client_id": "139751156265-mvroahocc1d4g7e848q44f1uqk0e78mc.apps.googleusercontent.com",
"scopes": [
"https://www.googleapis.com/auth/spreadsheets"
]
},
"content_security_policy": "script-src 'self' 'unsafe-eval' https://apis.google.com; object-src 'self'"
}