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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743984975a4538885.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信