Weibo: A Nodejs SDK for Weibo API

Node-weibo

A weibo(like twitter) API SDK, use on browser client and nodejs server.

Nodejs Install

$ npm install weibo

How to use

entry.js

var weibo = require('weibo');

// change appkey to yours
var appkey = 'your appkey';
var secret = 'your app secret';
var oauth_callback_url = 'your callback url';
weibo.init('weibo', appkey, secret, oauth_callback_url);

var user = { blogtype: 'weibo' };
var cursor = {count: 20};
weibo.public_timeline(user, cursor, function (err, statuses) {
  if (err) {
    console.error(err);
  } else {
    console.log(statuses);
  }
});

Demo on nodejs and browser just the same code.

Thanks for browserify, let us to use the same code on nodejs and browser.

Browser: Phonegap, Chrome extension or node-webkit.

NOTICE: browser must enable cross-domain request.

browserify to bundle.js

$ browserify entry.js -o bundle.js

Include bundle.js to your html.

<html>
  <head>
    <title>Weibo Hello world</title>
    <script src="bundle.js"></script>
  </head>
  <body>
    Hello world.
  </body>
</html>

Use weibo.oauth middleware

handler oauth login middleware, use on connect, express.

/**
 * oauth middleware for connect
 *
 * example:
 *
 *  connect(
 *    connect.query(),
 *    connect.cookieParser('I\'m cookie secret.'),
 *    connect.session({ secret: "oh year a secret" }),
 *    weibo.oauth()
 *  );
 *
 * @param {Object} [options]
 *   - {String} [homeUrl], use to create login success oauth_callback url with referer header,
 *     default is `'http://' + req.headers.host`;
 *   - {String} [loginPath], login url, default is '/oauth'
 *   - {String} [logoutPath], default is '/oauth/logout'
 *   - {String} [callbackPath], default is login_path + '/callback'
 *   - {String} [blogtypeField], default is 'type',
 *       if you want to connect weibo, login url should be '/oauth?type=weibo'
 *   - {Function(req, res, callback)} [afterLogin], when oauth login success, will call this function.
 *   - {Function(req, res, callback)} [beforeLogout], will call this function before user logout.
 */

Example: A simple web with oauth login.

var connect = require('connect');
var weibo = require('../');

/**
 * init weibo api settings
 */

weibo.init('weibo', '$appkey', '$secret');
weibo.init('tqq', '$appkey', '$secret');
weibo.init('github', '$ClientID', '$ClientSecret');

/**
 * Create a web application.
 */

var app = connect(
  connect.query(),
  connect.cookieParser('oh year a cookie secret'),
  connect.session({ secret: "oh year a secret" }),
  // using weibo.oauth middleware for use login
  // will auto save user in req.session.oauthUser
  weibo.oauth({
    loginPath: '/login',
    logoutPath: '/logout',
    blogtypeField: 'type',
    afterLogin: function (req, res, callback) {
      console.log(req.session.oauthUser.screen_name, 'login success');
      process.nextTick(callback);
    },
    beforeLogout: function (req, res, callback) {
      console.log(req.session.oauthUser.screen_name, 'loging out');
      process.nextTick(callback);
    }
  }),
  connect.errorHandler({ stack: true, dump: true })
);

app.use('/', function (req, res, next) {
  var user = req.session.oauthUser;
  res.writeHeader(200, { 'Content-Type': 'text/html' });
  if (!user) {
    res.end('Login with <a href="/login?type=weibo">Weibo</a> | \
      <a href="/login?type=tqq">QQ</a> | \
      <a href="/login?type=github">Github</a>');
    return;
  }
  res.end('Hello, <img src="' + user.profile_image_url + '" />\
    <a href="' + user.t_url +
    '" target="_blank">@' + user.screen_name + '</a>. ' +
    '<a href="/logout">Logout</a>');
});

app.listen(8088);
console.log('Server start on http://localhost:8088/');

Test

$ npm install
$ npm test

jscoverage: 79%

Authors

Below is the output from git-summary.

$ git summary

 project  : node-weibo
 repo age : 3 years
 active   : 73 days
 commits  : 173
 files    : 53
 authors  :
   156  fengmk2                 90.2%
     7  hpf1908                 4.0%
     3  chemzqm                 1.7%
     2  QLeelulu                1.2%
     1  hbbalfred               0.6%
     1  im007boy                0.6%
     1  iwillwen                0.6%
     1  mk2                     0.6%
     1  xydudu                  0.6%

Please see the API Documents first.

Supports APIs


Download Details:

Author: node-modules
Source Code: https://github.com/node-modules/weibo 
License: The MIT License

#node #nodejs #modules #sdk 

Weibo: A Nodejs SDK for Weibo API
1.05 GEEK