安裝
此程式庫透過npm發布。透過以下命令安裝googleapis及其依賴
$ npm install googleapis
完整的API支援清單 https://developers.google.com/apis-explorer
使用
例1: 透過Google短位址取得完整位址
var google = require('googleapis'); var urlshortener = google.urlshortener('v1'); var params = { shortUrl: 'http://goo.gl/xKbRu3' }; // get the long url of a shortened url urlshortener.url.get(params, function (err, response) { console.log('Long url is', response.longUrl); });
例2: 登入授權
此範例整合OAuth2認證,可以讓你取得到使用者的存取Token並刷新此Token防止會話過期。
var google = require('googleapis'); var plus = google.plus('v1'); var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); // Retrieve tokens via token exchange explained above or set them: oauth2Client.setCredentials({ access_token: 'ACCESS TOKEN HERE', refresh_token: 'REFRESH TOKEN HERE' }); plus.people.get({ userId: 'me', auth: oauth2Client }, function(err, response) { // handle err and response });
完整的登入授權範例。 https://github.com/google/google-api-nodejs-client/blob/master/examples/oauth2.js
例3: 文件上傳
var fs = require('fs'); var drive = google.drive({ version: 'v2', auth: oauth2Client }); drive.files.insert({ resource: { title: 'testimage.png', mimeType: 'image/png' }, media: { mimeType: 'image/png', body: fs.createReadStream('awesome.png') // read streams are awesome! } }, callback);
問題解答?
如有任何問題可到 Stackoverflow 提問
如果發現漏洞可到GitHub提交 Issue