PUT request in Chrome Extension using Google API not rendering

房东的猫 提交于 2020-01-03 16:44:49

问题


I'm stuck at this point of my code wherein I have successfully called the Sheets API using PUT request, but it's not rendering on the Google Sheet.

Here is my code where I use both PUT and GET requests to see if the data changed:

background.js

chrome.identity.getAuthToken({ 'interactive': true }, getToken);

function getToken(token) {

console.log('this is the token: ', token);

  var params = {
    "range":"Sheet1!A1:B1",
    "majorDimension": "ROWS",
      "values": [
      ["Hi","Crush"]
    ],
  }
  let init = {
    method: 'PUT',
    async: true,
    data: params,
    headers: {
      Authorization: 'Bearer ' + token,
      'Content-Type': 'application/json'
    },
    'contentType': 'json',
  };
  fetch(
      "https://sheets.googleapis.com/v4/spreadsheets/1efS6aMlPFqHJJdG8tQw-BNlv9WbA21jQlufsgtMsUmw/values/Sheet1!A1:B1?valueInputOption=USER_ENTERED",
      init)
      .then((response) => console.log(response))

      let request = {
        method: 'GET',
        async: true,
        headers: {
          Authorization: 'Bearer ' + token,
          'Content-Type': 'application/json'
        },
        'contentType': 'json',
      };
      fetch(
          "https://sheets.googleapis.com/v4/spreadsheets/1efS6aMlPFqHJJdG8tQw-BNlv9WbA21jQlufsgtMsUmw/values/Sheet1!A1:B1",
          request)
          .then((response) => response.json())
          .then(function(data) {
            console.log(data)
          });
}

Here's the screenshot of my Google Sheet, the data didn't change. The status of the PUT request is 200 and it seems the data is still Hello World in A1:B1:

Here's the log:

Do you have any idea what's missing here?


回答1:


How about this modification? Please modify the object of init as follows.

From:

data: params,

To:

body: JSON.stringify(params),

Reference:

  • Using Fetch


来源:https://stackoverflow.com/questions/55612194/put-request-in-chrome-extension-using-google-api-not-rendering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!