最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Sheet API : "message": "The request is missing a valid API key." - Stack

programmeradmin3浏览0评论

I'm trying to make an extencion that will take some data from page and send it to google sheet. My manifest:

"oauth2": {
        "client_id": "client_id",
        "scopes": [""] 
    },
    "permissions": [
        "activeTab",
        "tabs", 
        "https://*.*/*",
        "storage",
        "declarativeContent",
        "identity",
        "/",
        "/*",
        "/",
        "/",
        "/"
    ],
    "key": {key}

background page:

var tokenS;
chrome.runtime.onInstalled.addListener(function() {
  chrome.storage.sync.set({number: value}, function(data) {
  });
    chrome.identity.getAuthToken({
        interactive: false
    }, function(token) { 
        tokenS = token
        console.log(token)
    });
});
function writeData(request, sender){
    myspreadsheetId = "myspreadsheetId " 
    var params = {
        "range":"Sheet1!A1:A",
        "majorDimension": "ROWS",
        "values": [
            [request.name]
        ],
    }
    var xhr = new XMLHttpRequest();
    xhr.open('PUT', '/' + myspreadsheetId + '/values/Sheet1!A1:A1?valueInputOption=USER_ENTERED');
    console.log(tokenS)
    xhr.setRequestHeader('Authorization', 'Bearer '+tokenS);
    xhr.send(JSON.stringify(params));
};
chrome.runtime.onMessage.addListener(writeData)

I receive next error:

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "status": "PERMISSION_DENIED"
  }
}

at google cloud platform I see a 100% error ratio (). And I do not understand what I'm doing wrong. Pleasse help.

I'm trying to make an extencion that will take some data from page and send it to google sheet. My manifest:

"oauth2": {
        "client_id": "client_id",
        "scopes": ["https://www.googleapis./auth/spreadsheets"] 
    },
    "permissions": [
        "activeTab",
        "tabs", 
        "https://*.*/*",
        "storage",
        "declarativeContent",
        "identity",
        "https://ssl.gstatic./",
        "https://www.googleapis./*",
        "https://accounts.google./",
        "https://sheets.googleapis./",
        "https://www.googleapis./auth/spreadsheets/"
    ],
    "key": {key}

background page:

var tokenS;
chrome.runtime.onInstalled.addListener(function() {
  chrome.storage.sync.set({number: value}, function(data) {
  });
    chrome.identity.getAuthToken({
        interactive: false
    }, function(token) { 
        tokenS = token
        console.log(token)
    });
});
function writeData(request, sender){
    myspreadsheetId = "myspreadsheetId " 
    var params = {
        "range":"Sheet1!A1:A",
        "majorDimension": "ROWS",
        "values": [
            [request.name]
        ],
    }
    var xhr = new XMLHttpRequest();
    xhr.open('PUT', 'https://sheets.googleapis./v4/spreadsheets/' + myspreadsheetId + '/values/Sheet1!A1:A1?valueInputOption=USER_ENTERED');
    console.log(tokenS)
    xhr.setRequestHeader('Authorization', 'Bearer '+tokenS);
    xhr.send(JSON.stringify(params));
};
chrome.runtime.onMessage.addListener(writeData)

I receive next error:

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "status": "PERMISSION_DENIED"
  }
}

at google cloud platform I see a 100% error ratio (http://prntscr./jvfl0c). And I do not understand what I'm doing wrong. Pleasse help.

Share Improve this question asked Jun 15, 2018 at 18:31 AllexFAllexF 611 gold badge1 silver badge6 bronze badges 4
  • Well you API key seems to be invalid. – Liora Haydont Commented Jun 15, 2018 at 18:40
  • @ Liora Haydont as I know "There are two ways to identify your application: using an OAuth 2.0 token (which also authorizes the request) and/or using the application's API key."(developers.google./sheets/api/guides/authorizing) I'm using OAuth2.0 everything should work. – AllexF Commented Jun 16, 2018 at 4:24
  • Are you authenticated with OAuth2.0 before making a request? – ReyAnthonyRenacia Commented Jun 18, 2018 at 9:14
  • @nogui yes, I get token: 'chrome.identity.getAuthToken({ interactive: false }, function(token) { tokenS = token console.log(token) });' and then I use it: 'xhr.setRequestHeader('Authorization', 'Bearer '+tokenS);' – AllexF Commented Jun 18, 2018 at 17:57
Add a ment  | 

2 Answers 2

Reset to default 3

You're meant to replace the second 'key' in "key": {key}(on the last line of the first code sample) with an API Key you've generated for your project in the Google Cloud Console. To Generate your API Key you need to navigate to the credentials page and click on create credentials >> API key.


Firstly, ensure that your project of choice is selected Example


Next; from the 'Navigation Menu' >> 'APIs & Services' >> 'Crendentials' >> 'Create Credentials' >> 'API key'

that is case sensitive please check it whether it is Key or key

发布评论

评论列表(0)

  1. 暂无评论