Giter VIP home page Giter VIP logo

node-weibo's Introduction

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

Supports:

tapi SDK api base on tsina(weibo) api document: http://open.weibo.com/

Requires:

  • browser client: jquery(for ajax request)
  • server: nodejs

Nodejs Install

$ npm install weibo

How to use

Browser

// Include the `weibo.js` javascript files:

<script type="text/javascript" src="../weibo.js"></script>
<script type="text/javascript">
// load all the lib scripts with urlprefix where the `weibo` directory you put into 
weibo.load('/public/js/weibo');

var tapi = weibo.tapi;
var appkey = 'your appkey', secret = 'your app secret';
var oauth_callback_url = 'your callback url' || 'oob';
tapi.init('tsina', appkey, secret, oauth_callback_url);
tapi.public_timeline({}, function(error, data, xhr) {
  if (error) {
    console.error(error);
  } else {
    console.log(data);
  }
});

</script>

Server

var tapi = require('weibo').tapi;
// change appkey to yours
var appkey = 'your appkey', secret = 'your app secret';
var oauth_callback_url = 'your callback url' || 'oob';
tapi.init('tsina', appkey, secret, oauth_callback_url);
tapi.public_timeline({}, function(error, data, response) {
  if (error) {
      console.error(error);
  } else {
      console.log(data);
  }
});

Use oauth_middleware

handler oauth login middleware, use on connect, express.

/**
 * oauth middleware for connect
 *
 * example:
 *
 *  connect(
 *    connect.query(),
 *    connect.cookieParser(),
 *    connect.session({ secret: "oh year a secret" }),
 *    weibo.oauth()
 *  );
 *
 * @param {Object} options
 *   - `home_url`: use to create login success oauth_callback url with referer header, 
 *     default is `'http://' + req.headers.host`;
 *   - `login_path`: login url, default is '/oauth'
 *   - `logout_path`: default is '/oauth/logout'
 *   - `callback_path`: default is login_path + '_callback'
 *   - `blogtype_field`: default is 'blogtype', 
 *     if you want to connect weibo, login url should be '/oauth?blogtype=weibo'
 */

Example: A simple web with oauth login.

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

/**
 * init weibo api settings
 */ 

weibo.init('weibo', 'appkey', 'secret');

/**
 * Create a web application.
 */

var app = connect(
  connect.query(),
  connect.cookieParser(),
  connect.session({ secret: "oh year a secret" }),
  // using weibo.oauth middleware for use login
  // will auto save user in req.session.oauthUser
  weibo.oauth({
    login_path: '/login',
    logout_path: '/logout',
    blogtype_field: 'type'
  }),
  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('<a href="/login?type=weibo">Login</a> first, please.');
    return;
  }
  res.end('Hello, <a href="' + user.t_url + 
    '" target="_blank">@' + user.screen_name + '</a>. ' + 
    '<a href="/logout">Logout</a>');
});

app.listen(8080);

node-weibo's People

Contributors

fengmk2 avatar hpf1908 avatar qleelulu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.