Я решил свою проблему !!!
Я пропустил эту часть документации OAuth2: аутентификация пользователей с помощью Google . Я так глуп, хахаха!
Хорошо, вот как я интегрирую Google Sheets в свое расширение. Я использовал этот метод для начального:
GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}
Это мой обновленный manifest.json
:
{
"name": "Sample Extension",
"version": "1.0",
"description": "Hello World",
"permissions": ["identity", "https://docs.google.com/spreadsheets/"],
"author": "Jess",
"background": {
"scripts": ["background.js"],
"persistent": true
},
"content_scripts": [{
"matches": ["file:///*"],
"js" : ["popup.js"]
}],
"browser_action": {
"default_popup": "popup.html",
"default_icon": "images/get_started16.png",
"default_title": "This is a sample extension"
},
"oauth2": {
"client_id": "client_ID",
"scopes": [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/spreadsheets.readonly"
]},
"content_security_policy": "script-src 'self' 'unsafe-eval' https://apis.google.com/; object-src 'self'",
"manifest_version": 2
}
Это мой обновленный background.js
:
chrome.identity.getAuthToken({ 'interactive': true }, getToken);
function getToken(token) {
console.log('this is the token: ', token);
let init = {
method: 'GET',
async: true,
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
'contentType': 'json'
};
fetch(
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range},
init)
.then((response) => response.json())
.then(function(data) {
console.log(data)
});
}
Я успешно зарегистрировал данные из моего запроса.
Готово!