Google电子表格创建-Node.js [英] Google Spreadsheet Creation - Node.js

查看:108
本文介绍了Google电子表格创建-Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用使用node.js的Google电子表格api创建新的Google电子表格

I am trying to create new google spreadsheet using the google spreadsheet api using node.js

我设法使Google OAuth 2.0正常运行,在那里我获得了客户端的访问令牌.

I have managed to get the Google OAuth 2.0 working, where I am getting the access tokens for the clients.

现在,在搜索Google API文档时,有使用gData客户端库的示例,但没有提供指向node.js的指针

Now on searching the Google API docs there are example using the gData client library but nothing giving me pointers to node.js

这是我创建新的Google Spreadseet的发现

Here are my findings for creating a new google spreadhseet

  1. 手动上传电子表格,或者
  2. 使用可恢复的上传链接

可恢复的上传链接上没有太多信息.

There is not much information on the resumable upload link.

我可以看到HTTP发布请求和响应,但是我不明白如何在node.js中构造发布请求

I can see the HTTP Post Request and Response but I do not understand how to construct the post request in node.js

编辑-

我正在阅读 Google Apps平台

推荐答案

以下是使用 create 方法> Google Sheets API (当前为v4).

Here is how to do it with the create method of the Google Sheets API (currently v4).

此代码示例不使用任何第三方库,而是使用 googleapis:Google API的官方节点.js客户端

This code examples does not use any 3rd party libraries, it uses googleapis: Google API's official Node.js client

const google = require('googleapis');
const sheets = google.sheets('v4');

// authenticate, and store that authentication, in this example
// it is stored in a variable called `auth`. I am using JWT 
// authentication, but you can use the any form of authentication

const auth = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ['https://www.googleapis.com/auth/spreadsheets'], // make sure that your auth scope includes `spreadsheets`, `drive` or `drive.file`
  null
);

sheets.spreadsheets.create({
  auth: auth,
  resource: {
    properties: {
      title: 'Title of your new spreadsheet'
    }
  }
}, (err, response) => {
  if (err) {
    console.log(`The API returned an error: ${err}`);
    return;
  }

  console.log('Created a new spreadsheet:')
  console.log(response);
});

这篇关于Google电子表格创建-Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆