weiboodesdk是新浪微博Node.jsSDK。
特点api可配置化
接口采用promise
最少依赖,专注新浪微博OAuth2.0认证
使用方法安装
pm istall iweibo配置引入iweibovar iweibo = require('iweibo'); var Weibo = iweibo.Weibo;配置app信息iweibo.set(ame,optios);//设置单条
iweibo.set(optiosObject); //设置多条
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_toke', api_url: 'https://api.weibo.com/2/' }配置api接口iweibo.setAPI(apiame,optios);//设置单条api
iweibo.setAPI(optiosObject); //设置多条api
iweibo.setAPI('statuses/update', { method: 'post', params: { status: 'hello, world', visible: 0 } });配置下微博接口(由于太多,并且不时更新,所以我就没全配置),配置下自己使用的接口,方法参考下件,基本如下:
'接口名称': { method: 'get', //请求方法,post或者get(get可省略),参考api文档 params: { //默认参数,不用填写appkey和access_toke,程序会自动补上 } }可以讲接口统一写到一个jso或者js文件中,然后使用require引入,直接给setAPI传入
使用参考examples/app.js文件(需要先在本目录执行pmistall安装依赖模块)
修改host,添加下面内容:
127.0.0.1 testapp.c进入ope.weibo.com设置应用回调地址到https://testapp.c/callbak
获取登录链接weibo.getAuthorizeURL(backURL);获取access_tokeweibo.getAccessToke('code', { code: code, redirect_uri: backURL }).doe(fuctio(err, data) { var realpath = templateDir + 'callback.html'; html = fs.readFileSyc(realpath); data = JSON.parse(data); data.refresh_toke = data.refresh_toke || ''; req.sessio.refresh_toke = data.refresh_toke; req.sessio.access_toke = data.access_toke; req.sessio.uid = data.uid; html = bdTemplate(html, data); res.ed(html); }).fail(fuctio(err, data) { var html; if (err) { html = fs.readFileSyc(templateDir + 'error.html'); } res.ed(html); });使用api接口//所有API都支持promise接口 weibo.api('users/show', urlObj).doe(fuctio(err, result) { cosole.log(result); res.ed(JSON.strigify(result)); });测试方法进入examples
修改cofig.jso,回调地址需要在ope.weibo.com配置好,然后修改自己的host,将回调地址指到127.0.0.1
执行pmistall
访问自己在cofig.jso配置的网站
评论