Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Google Sheets API V4 API键创建电子表格_Javascript_Node.js_Request_Google Sheets Api - Fatal编程技术网

Javascript 使用Google Sheets API V4 API键创建电子表格

Javascript 使用Google Sheets API V4 API键创建电子表格,javascript,node.js,request,google-sheets-api,Javascript,Node.js,Request,Google Sheets Api,我已经知道如何从我创建的预算电子表格中读取值,但是我不知道如何使用Sheets API V4创建新的电子表格。我已经为这个问题挣扎了5个月了,以前有人解决过这个问题吗 这是我的密码: // READ - WORKING! router.get("/get", (req, res) => { var id = '1LoSF_4Z9aoiVvDsjFV9CMOd--vvz3fERfOPajVb2sv8'; var params = 'https://sheets.googleapi

我已经知道如何从我创建的预算电子表格中读取值,但是我不知道如何使用Sheets API V4创建新的电子表格。我已经为这个问题挣扎了5个月了,以前有人解决过这个问题吗

这是我的密码:

// READ - WORKING!
router.get("/get", (req, res) => {
  var id = '1LoSF_4Z9aoiVvDsjFV9CMOd--vvz3fERfOPajVb2sv8';  
  var params = 'https://sheets.googleapis.com/v4/spreadsheets/?key='
  var url = params + apiKey;
  request(`https://sheets.googleapis.com/v4/spreadsheets/${id}?key=${apiKey}`, (error, response, body) => {
    console.log("Body", body);
  });
})

// Create - NOT WORKING!
router.post('/create', (req,res)=>{
  request({
    method: 'POST',
    uri: `https://sheets.googleapis.com/v4/spreadsheets?fields=properties%2Ftitle&key=${apiKey}`
  }, (error, response, body)=>{

    console.log(body);
    //Logs the body of the newly created spreadsheet

  })
})
我使用了谷歌API浏览器的指南,你可以在这里找到:


谢谢大家!

这次修改怎么样

修改点:
  • 使用访问令牌添加
    标题
  • 添加
    正文
    ,以给出所创建电子表格的标题
修改脚本: 注:
  • 使用此脚本时,请使用包含
    https://www.googleapis.com/auth/spreadsheets
    到作用域。此范围用于创建电子表格
参考:

    • 我真的让它工作了!与前面的回答类似(谢谢Tanaike!!!)


      API键不能使用工作表API的创建方法。那么使用OAuth2和服务帐户怎么样?谢谢!很高兴知道这一点。我在另一个应用程序中设置了OAuth2,该应用程序授权Google Sheets的范围。如何执行POST请求,将OAuth访问令牌传递给sheets api并创建电子表格?你有一个如何做到这一点的例子吗?我理解这个概念,但我从未见过它被使用。谢谢你的回复。我能问一下你的情况吗?您已经从OAuth2进程检索到访问令牌。您可以使用访问令牌。我的理解正确吗?是的,那是正确的。在使用OAuth进行身份验证后,我从Google返回了访问令牌。有了这个令牌,我现在可以在自己的驱动器或任何登录的人中发布创建电子表格。你还有其他问题吗?谢谢你的回答。我将修改后的脚本作为答案发布。你能确认一下吗?在此修改后的脚本中,将使用检索到的访问令牌。当您使用Sheets API创建电子表格时,请包括
      https://www.googleapis.com/auth/spreadsheets
      到作用域。如果这不是您想要的,我很抱歉。可能最好也请求创建的Google Sheets文件的
      电子表格ID
      ,以便可以立即使用它(而不仅仅是
      标题
      字段)@tehhowch谢谢您的评论。我遵循用户的查询参数。但我也认为你的评论对用户的情况很有用。所以我更新了剧本,明白了!谢谢,你好!如何使用API密钥管理它?我不想一直刷新令牌…@M.Mariscal不幸的是,API密钥无法使用Sheets API的创建方法。对于目前的规格,我深表歉意。我很高兴您的问题得到了解决。但很抱歉我帮不了你。如果我看到你的回答,我会解决的,因为他们几乎是一样的。你解决了,谢谢!谢谢如果您的问题已解决,请将其视为已解决的问题。这对有相同问题的用户很有用。您好,您能告诉我您是如何获得访问令牌的吗?
      request({
          method: 'POST',
          uri: 'https://sheets.googleapis.com/v4/spreadsheets?fields=properties%2Ftitle%2CspreadsheetId',
          headers: {
              'Content-Type': 'application/json',
              'Authorization': 'Bearer ' + accessToken,
          },
          body: JSON.stringify({properties: {title: "sampleTitle"}}),
      }, (error, response, body) => {
          console.log(body);
      });
      
      request({
      method: 'POST',
      url: 'https://sheets.googleapis.com/v4/spreadsheets',
      headers:{
        'Authorization': 'Bearer (access token goes here)'
      },
      body: JSON.stringify({
        properties: {
          title: "Spreadsheet Title Goes Here"
        }
      })}, (error, response, body)=>{
          if(!error && response.statusCode == 200){
            var info = JSON.parse(body);
            console.log(info);
          } else {
            console.log(error);
          }
        })