我正在开发chrome扩展应用程序,因此我已经创建了API密钥和OAuth2客户端ID,并且能够毫无问题地获取Google驱动器文件,但是当我尝试访问电子表格时,我只是收到此错误消息说我的API密钥无效。我已包含所有必要的作用域,确保已启用Google云端硬盘和Google表格,并删除并重新创建了我的API密钥,但仍然会遇到相同的错误。请帮忙。
window.onload = function() {
document.getElementById('pi').addEventListener('click', function() {
chrome.identity.getAuthToken({interactive: true}, async function(token){
//setToken(token);
let init = {
method: 'GET',
async: true,
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
'contentType': 'json'
};
//var theFoundURL = await getUrl2();
//console.log("THE FOUND URL: " + theFoundURL);
fetch(
"https://sheets.googleapis.com/v4/spreadsheets/1b9NsKlhFeCDHXHGekQjhVxRmKfnijRs8Mkx9RFr9DIs/values/Sheet1A1%3AB1?key={MYAPIKEY}",
init)
.then((response) => response.json())
.then(function(data) {
console.log(data)
let nowD = "1"; //getDate();
if(nowD != data[0]){
console.log("FALSE");
}
else console.log("TRUE");
});
});
}
);
}
JavaScript ^
{
"manifest_version": 2,
"name": "G",
"version": "1.0",
"browser_action":{
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'",
"permissions": [
"tabs",
"storage",
"activeTab",
"alarms",
"identity"
],
"oauth2": {
"client_id": "MYID",
"scopes": [
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/drive.metadata.readonly",
"https://www.googleapis.com/auth/drive.metadata",
"https://www.googleapis.com/auth/drive.appdata",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/spreadsheets.readonly"
]
},
"key": "MYKEY"
}
Manifest.json ^
<html>
<head>
<title>Spreadsheet Test</title>
<style>
button {
padding: 10px;
background-color: #3C79F8;
display: inline-block;
}
</style>
<script type="text/javascript" src= popup.js></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body>
<button id = "pi">Test</button>
<div id="friendDiv"></div>
</body>
</html>
Popup.html ^
1。 Initial state before logging into chrome and allowing access
编辑:
很抱歉,我的问题不够清楚。我想要的行为是让我的扩展程序检索用户Google电子表格的特定单元格中的文本,但是我面临的问题是当使用我的帐户对其进行测试并定义特定的电子表格以从中检索数据时扩展名不能访问它。我仍然有这个问题,而且仍然很困。就像我之前说的,我在扩展程序中使用了相同的JavaScript格式/语法来访问Google驱动器上的文件,并获得完美的结果而没有任何问题。我已经在其API参考网站中添加了使用Google Spreadsheets的必要范围。如果您至少能确定我的问题,将不胜感激。
function findSpreadSheet(token){
theSpreadsheetUrl = null;
let init = {
method: 'GET',
async: true,
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
'contentType': 'json'
};
return new Promise( function (resolve, reject) {
fetch(
"https://www.googleapis.com/drive/v3/files?q=mimeType%3D'application%2Fvnd.google-apps.spreadsheet'&key={MYAPIKEY}",
init)
.then((response) => response.json())
.then(function(data) {
console.log(data)
for(let i = 0; i < data.files.length; i++) {
if(SEARCH_FOR == data.files[i].name){
theSpreadsheetUrl = data.files[i].id;
console.log("NOW");
console.log(theSpreadsheetUrl);
resolve(theSpreadsheetUrl);
}
}
reject(null);
});
});
}
^我的有效JavaScript,如果您想知道的话,我会使用它来检索Google驱动器文件