weibo-node-sdk新浪微博 Node.js SDK
weibo node sdk 是新浪微博 Node.js SDK。
特點(diǎn)
-
api 可配置化
-
接口采用 promise
-
最少依賴,專注新浪微博 OAuth2.0 認(rèn)證
使用方法
安裝
npm install iweibo
配置
引入iweibo
var iweibo = require('iweibo'); var Weibo = iweibo.Weibo;
配置app信息
-
iweibo.set(name, options); //設(shè)置單條
-
iweibo.set(optionsObject); //設(shè)置多條
iweibo.set({
appkey: 'xxx',
appsecret: 'xxxxxxxxxx'
})
支持的配置:
var CONFIG = {
appkey: '',
appsecret: '',
oauth_host: 'https://api.weibo.com/oauth2/authorize',
access_url: 'https://api.weibo.com/oauth2/access_token',
api_url: 'https://api.weibo.com/2/'
}
配置api接口
-
iweibo.setAPI(apiname, options); //設(shè)置單條api
-
iweibo.setAPI(optionsObject); //設(shè)置多條api
iweibo.setAPI('statuses/update', {
method: 'post',
params: {
status: 'hello, world',
visible: 0
} });
配置下微博接口(由于太多,并且不時(shí)更新,所以我就沒(méi)全配置),配置下自己使用的接口,方法參考下件,基本如下:
'接口名稱': {
method: 'get', //請(qǐng)求方法,post或者get(get可省略),參考api文檔
params: { //默認(rèn)參數(shù),不用填寫appkey和access_token,程序會(huì)自動(dòng)補(bǔ)上
} }
可以講接口統(tǒng)一寫到一個(gè)json或者js文件中,然后使用 require 引入,直接給 setAPI 傳入
使用
參考 examples/app.js 文件(需要先在本目錄執(zhí)行 npm install 安裝依賴模塊)
修改host,添加下面內(nèi)容:
127.0.0.1 testapp.cn
進(jìn)入 open.weibo.com 設(shè)置應(yīng)用回調(diào)地址到 http://testapp.cn/callbak
獲取登錄鏈接
weibo.getAuthorizeURL(backURL);
獲取access_token
weibo.getAccessToken('code', {
code: code,
redirect_uri: backURL }).done(function(err, data) {
var realpath = templateDir + 'callback.html';
html = fs.readFileSync(realpath);
data = JSON.parse(data);
data.refresh_token = data.refresh_token || '';
req.session.refresh_token = data.refresh_token;
req.session.access_token = data.access_token;
req.session.uid = data.uid;
html = bdTemplate(html, data);
res.end(html); }).fail(function(err, data) {
var html;
if (err) {
html = fs.readFileSync(templateDir + 'error.html');
}
res.end(html); });
使用api接口
//所有API都支持promise接口
weibo.api('users/show', urlObj).done(function(err, result) {
console.log(result);
res.end(JSON.stringify(result));
});
測(cè)試方法
-
進(jìn)入
examples -
修改
config.json,回調(diào)地址需要在open.weibo.com配置好,然后修改自己的host,將回調(diào)地址指到127.0.0.1 -
執(zhí)行
npm install -
訪問(wèn)自己在
config.json配置的網(wǎng)站
評(píng)論
圖片
表情
