Google官方支持的NodeJS訪問API,提供后臺登錄授權
安裝
此庫通過npm發(fā)布。通過以下命令安裝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 提問
如果發(fā)現(xiàn)漏洞可到GitHub上提交 Issue
相關文章
node.js+postman+mongodb搭建測試注冊接口的實現(xiàn)
本文主要介紹了node.js+postman+mongodb搭建測試注冊接口的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
win系統(tǒng)下nodejs環(huán)境安裝配置
這篇文章主要介紹了win系統(tǒng)下nodejs環(huán)境安裝配置的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
node.js express JWT token生成與校驗的實現(xiàn)
本文主要介紹了node.js express JWT token生成與校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-12-12
nodejs個人博客開發(fā)第四步 數(shù)據(jù)模型
這篇文章主要為大家詳細介紹了nodejs個人博客開發(fā)的數(shù)據(jù)模型,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04

