如何使用api密钥访问Google云端硬盘 [英] How to use api key to access the Google Drive

查看:363
本文介绍了如何使用api密钥访问Google云端硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用apiGoogle Node.js客户端库访问Google Drive API,以便可以将一些文件上传到我的驱动器帐户.

I am trying to use apiGoogle Node.js client library to access the Google Drive API so I can upload some files to my drive account.

到目前为止,我的代码:

My code so far:

var drive = google.drive({ version: 'v3' });
drive.apiKey = "API_KEY"
drive.setScope = "https://www.googleapis.com/auth/drive.file"

drive.files.create({
    resource: {
        name: 'text.text',
        mimeType: 'text/plain',
        parents:['1ATscm2nsE9KpjA66iXHB9jnMrbyik7PP']
    },
    media: {
        mimeType: 'text/plain',

        body: fs.createReadStream('files/text.text')

    }
});

错误:

 errors: 
   [ { domain: 'global',
       reason: 'required',
       message: 'Login Required',
       locationType: 'header',
       location: 'Authorization' } ] }

有人可以帮助解决此问题吗?

Can anybody help to resolve this issue?

推荐答案

您似乎缺少授权.首先,您需要在开发人员控制台中打开Drive API.然后,您可以将其与替换为您的凭据的以下代码一起使用.

It looks like you are missing the authorization. First you need to turn on the Drive API in the developers console. Then you can use it with the following code replaced by your credentials.

var {google} = require('googleapis');
var OAuth2 = google.auth.OAuth2; 
var oauth2Client = new OAuth2(  
  YOUR_CLIENT_ID,   
  YOUR_CLIENT_SECRET,   
  YOUR_REDIRECT_URL 
);
var drive = google.drive({ 
  version: 'v3',
  auth: oauth2Client
});

您可以在 github 上查看此部分. a>如果您对此还有其他疑问.

You can check out this section on github if you have more questions about this.

或者,如果没有,可以在此处 a>.

Or if you haven't you can check out their docs here.

这篇关于如何使用api密钥访问Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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