2014-09-10 100 views
1

我有兴趣从我的Google Drive访问我的文件(或者只是一个文件夹) - 并且通过服务器端代码创建(并授予)访问权限。我对通过浏览器登录不感兴趣。使用OAuth 2.0实现服务器到服务器应用程序的节点

我创建了OAuth的服务帐户,下载的P12文件,并将其转换为.PEM文件..

,但我无法看到的Node.js任何实例来做到这一点。我在github上看到了一些使用OAuth访问Google Drive的示例 - 但公平地说,其中很多都不起作用 - 这非常令人沮丧。

谷歌没有为此的Node.js客户端API库。

如果任何人有任何想法,我很乐意提供任何帮助或指向正确的方向。

感谢所有帮助...

pandafinity

回答

0

这是一个让我通过它用同样的方式谷歌分析API集成时的指导和代码。它应该为驱动器工作太:

https://gist.github.com/PaquitoSoft/4451865

+0

嗨:) - 欣赏你说,但我真的不能看到谷歌Drive..so连接中使用谷歌,OAuth的智威汤逊这进一步,但不是最终得到了我至今。感谢您的想法:) – Pandafinity 2014-09-11 12:35:27

0

我进一步得到:

使用Node包“谷歌OAuth的智威汤逊”我已经成功地验证了我的请求,并接收返回的访问令牌。

var googleAuth = require('google-oauth-jwt'); 

// obtain a JWT-enabled version of request 
var request = googleAuth.requestWithJWT(); 
var TokenCache = googleAuth.TokenCache; 
var tokens = new TokenCache(); 

tokens.get({ 
    // use the email address of the service account, as seen in the API console 
    email: 'google-generated-email', 
    // use the PEM file we generated from the downloaded key 
    keyFile: 'pemfile.pem', 
    // specify the scopes you wish to access 
    scopes: ['https://www.googleapis.com/auth/drive.readonly'] 
}, function (err, token) { 
    console.log('Google API with JWT Request'); 
    console.log('---------------------------'); 
    console.log('The GoogleDrive Token is : ' + token); 
}); 

googleAuth.authenticate({ 
    // use the email address of the service account, as seen in the API console 
    email: 'google-generated-email', 
    // use the PEM file we generated from the downloaded key 
    keyFile: 'pemfile.pem', 
    // specify the scopes you wish to access 
    scopes: ['https://www.googleapis.com/auth/drive.readonly'] 
}, function (err, token) { 
    console.log('Google API: Authenticated'); 
    console.log('------------------------'); 
    console.log('The GoogleDrive Token is : ' + token); 
}); 

另一件事谷歌与OAuth的智威汤逊可以是使用JWT发送一个请求 - 但我对咖啡低或并不真正理解什么回来了(可能是后者)。

我用下面的代码发出请求:

request({ 
    url: 'https://www.googleapis.com/drive/v2/files', 
    jwt: { 
    // use the email address of the service account, as seen in the API console 
    email: 'google-generated-email', 
    // use the PEM file we generated from the downloaded key 
    keyFile: 'pemfile.pem', 
    // specify the scopes you wish to access - each application has different scopes 
    scopes: ['https://www.googleapis.com/auth/drive.readonly'] 
    } 
}, function (err, res, body) { 
    console.log(JSON.parse(body)); 
}); 

的CONSOLE.LOG产生如下:

{ kind: 'drive#fileList', 
    etag: '"fk0AzBEIhUhhdZ8fZzKcL1hA5NE/oop8mnyogpISt5nktdy1MHxUDnc"', 
    selfLink: 'https://www.googleapis.com/drive/v2/files', 
    items: 
    [ { kind: 'drive#file', 
     id: '0B1ka-zLvU5tjc3RhcnRlcl9maWxl', 
     etag: '"fk0AzBEIhUhhdZ8fZzKcL1hA5NE/MTQxMDM1MTI0MTkzOA"', 
     selfLink: 'https://www.googleapis.com/drive/v2/files/0B1ka-zLvU5tjc3RhcnRlcl9maWxl', 
     webContentLink: 'https://docs.google.com/uc?id=0B1ka-zLvU5tjc3RhcnRlcl9maWxl&export=download', 
     alternateLink: 'https://docs.google.com/file/d/0B1ka-zLvU5tjc3RhcnRlcl9maWxl/edit?usp=drivesdk', 
     iconLink: 'https://ssl.gstatic.com/docs/doclist/images/icon_10_pdf_list.png', 
     thumbnailLink: 'https://lh6.googleusercontent.com/[somestuff]_ZFIkWw-che6o=s220', 
     title: 'How to get started with Drive', 
     mimeType: 'application/pdf', 
     labels: [Object], 
     createdDate: '2014-09-10T12:14:01.938Z', 
     modifiedDate: '2014-09-10T12:14:01.938Z', 
     markedViewedByMeDate: '1970-01-01T00:00:00.000Z', 
     version: '6', 
     parents: [Object], 
     downloadUrl: 'https://doc-0s-b4-docs.googleusercontent.com/docs/securesc/[somestuff]/0B1ka-zLvU5tjc3RhcnRlcl9maWxl?h=16653014193614665626&e=download&gd=true', 
     userPermission: [Object], 
     originalFilename: 'How to get started with Drive', 
     fileExtension: '', 
     md5Checksum: '2f215372c903f401e9e101d1d531e5dd', 
     fileSize: '3017667', 
     quotaBytesUsed: '0', 
     ownerNames: [Object], 
     owners: [Object], 
     lastModifyingUserName: '', 
     lastModifyingUser: [Object], 
     editable: true, 
     copyable: true, 
     writersCanShare: true, 
     shared: false, 
     appDataContents: false } ] } 

我试图让从一个 'children.list'文件夹(无论是根目录还是文件夹ID) - 但这不是它。有没有人有这种方法访问文件夹列表的任何想法?

感谢您的帮助:)

相关问题