2014-09-10 68 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) - 但這不是它。有沒有人有這種方法訪問文件夾列表的任何想法?

感謝您的幫助:)

相關問題