简体   繁体   English

如何通过Google云端硬盘Javascript OAuth2 API创建Google表格文档?

[英]How do I create a Google Sheets document via the Google Drive Javascript OAuth2 API?

I am trying to create a blank Google Sheets document in Google Drive from a browser based app. 我正在尝试通过基于浏览器的应用程序在Google云端硬盘中创建空白的Google表格文档。 I am able to create a blank file with an unknown file type, but have not been able to create a Google Sheets document. 我能够创建一个文件类型未知的空白文件,但无法创建Google表格文档。

My code is as follows: 我的代码如下:

function createfile() {
    var config = {
    'client_id': '<putyourclient_idhere>',
    'scope': 'https://www.googleapis.com/auth/drive'
    };
    gapi.auth.authorize(config, function() {

        var request = gapi.client.request({
            'path': '/upload/drive/v2/files',
            'method': 'POST'
        });

        var callback = function(file) {
            console.log(file)
        };

        request.execute(callback);


    });
}

I have reviewed the documentation here: https://developers.google.com/drive/v2/reference/files/insert 我在这里查看了文档: https : //developers.google.com/drive/v2/reference/files/insert

And the following StackOverflow posts: Creating empty spreadsheets in Google Drive using Drive API (in Python) How to create an empty Google doc / Spreadsheet 以下是StackOverflow的帖子: 使用Drive API在Google Drive中创建空电子表格(在Python中) 如何创建空Google文档/电子表格

I realise that I need to use a mimeType of 'application/vnd.google-apps.spreadsheet'. 我意识到我需要使用mimeType为“ application / vnd.google-apps.spreadsheet”。

I have also been able to successfully create a blank Google Sheets document using the APIs Explorer, but have been unable to determine how this is working. 我还能够使用API​​资源管理器成功创建空白的Google表格文档,但无法确定其工作方式。 It is doing a post request to: 它正在发出以下请求:

With a request payload of: 请求有效负载为:

{ "title": "This is a test", "mimeType": "application/vnd.google-apps.spreadsheet" } {“ title”:“这是一个测试”,“ mimeType”:“ application / vnd.google-apps.spreadsheet”}

But note that the URL is slightly different. 但是请注意,URL略有不同。

I have an easy way to do this with a product called Cloud Snippets (cloudward.com). 我有一种简单的方法,可以使用名为Cloud Snippets(cloudward.com)的产品来做到这一点。 You can create the snippet and embed into your web site. 您可以创建代码段并嵌入到您的网站中。

The snippet could be as easy as one command: 该代码段就像一个命令一样简单:

<# create record for spreadsheet "create sheet example";
   set a to "";
   set b to "";
#>

this creates a sheet with columns named "a" and "b" 这将创建一个包含名为“ a”和“ b”的列的工作表

you could actually add content: 您实际上可以添加内容:

<# create record for spreadsheet "create sheet example";
   set Name to "Bob";
   set Relation to "Uncle";
   set Email to "bob@example.com";
#>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在移动设备上正确执行Google oauth2 JavaScript工作流程? - How do I properly do a Google oauth2 javascript workflow on mobile? 如何使用Drive API创建空白的Google Spreadsheet? - How do I create a blank Google Spreadsheet using Drive API? 如何使用Google表格API在JavaScript中创建包含两个表格的Google电子表格? - How Can I create a Google spreadsheet with two sheets in javascript using Google sheets API? 调用Google Drive API会返回Invalid_grant OAuth2 - Calling Google Drive API returns Invalid_grant OAuth2 如何使用Javascript从Google Api检索服务帐户OAuth2令牌? - How can I retrieve a service account OAuth2 token from Google Api with Javascript? 使用Oauth2将REST用于Google云端硬盘 - Using REST for Google Drive using Oauth2 使用javascript API的Google Oauth2登录不适用于IE - Google Oauth2 sign in using javascript API is not working with IE Google通过OAuth2身份验证从JavaScript客户端联系API问题 - Google contacts API problems from JavaScript client with OAuth2 authentication 使用Google Drive API和Node.js创建Google文档 - Create a Google Document with Google Drive API and Node.js 如何在流星上使用Google Drive API上传文件? - How do I upload files via using Google Drive API on meteor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM