Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added provider for weibo.com #311

Merged
merged 1 commit into from

2 participants

@justan

This adds a provider for sina weibo( 新浪微博, with more than 300 million users) using oauth2.

@bnoguchi bnoguchi merged commit 8c11710 into bnoguchi:master
@bnoguchi
Owner

Thanks for the pull request. Can you also add any relevant documentation to the README?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 23, 2012
  1. @justan

    added sina weibo to providers

    justan authored
This page is out of date. Refresh to see the latest.
View
1  README.md
@@ -61,6 +61,7 @@ So far, `everyauth` enables you to login via:
<tr> <td> LDAP (experimental; not production-tested) <td>
<tr> <td> Windows Azure Access Control Service (ACS)<td> <a href="https://github.com/darrenzully">Dario Renzulli</a>, <a href="https://github.com/jpgarcia">Juan Pablo Garcia</a>, <a href="https://github.com/woloski">Matias Woloski</a> from <a href="http://blogs.southworks.net/">Southworks</a>
<tr><td><img src='https://www.dailycred.com/public/img/favicon.ico' style="vertical-align:middle">Dailycred <td> <a href='https://github.com/hstove'>Hank Stoever</a> at <a href='https://dailycred.com'>Dailycred.com</a>
+ <tr><td><img src='http://www.sinaimg.cn/blog/developer/wiki/LOGO_16x16.png' style="vertical-align:middle">Sina Weibo<td> <a href='https://github.com/justan'>justan</a>
</tbody>
</table>
View
6 example/conf.js
@@ -119,5 +119,9 @@ module.exports = {
, mendeley: {
consumerKey: 'Enter your consumer key here'
, consumerSecret: 'Enter your consumer secret here'
- }
+ }
+ , weibo: {
+ appId: '3350967939'
+ , appSecret: 'ef7f0a836d0ef315dca53e8d73816cc0'
+ }
};
View
11 example/server.js
@@ -52,6 +52,7 @@ var usersByMailchimpId = {};
var usersMailruId = {};
var usersByMendeleyId = {};
var usersByDcId = {};
+var usersByWeiboId = {};
var usersByLogin = {
'brian@example.com': addUser({ login: 'brian@example.com', password: 'password'})
};
@@ -419,6 +420,16 @@ everyauth
(usersByMailchimpId[mailchimpUser.user_id] = addUser('mailchimp', mailchimpUser));
})
.redirectPath("/");
+
+everyauth
+ .weibo
+ .appId(conf.weibo.appId)
+ .appSecret(conf.weibo.appSecret)
+ .findOrCreateUser( function (session, accessToken, accessTokenExtra, weiboUser){
+ return usersByWeiboId[weiboUser.uid] ||
+ (usersByWeiboId[weiboUser.uid] = addUser('weibo', weiboUser));
+ })
+ .redirectPath("/");
var app = express.createServer(
express.bodyParser()
View
4 example/views/home.jade
@@ -93,6 +93,8 @@
input(type='submit') Login
#dailycred-login
a(href='/auth/dailycred') Login with Dailycred
+ #weibo-login
+ a(href='/auth/weibo') Login with sina weibo
- else
h2 Authenticated
- if (everyauth.facebook)
@@ -181,5 +183,7 @@
p= JSON.stringify(everyauth.mendeley.user)
- if (everyauth['dailycred'])
p= JSON.stringify(everyauth.dailycred.user.email)
+ - if (everyauth['weibo'])
+ p= JSON.stringify(everyauth.weibo.user)
h3
a(href='/logout') Logout
View
74 lib/modules/weibo.js
@@ -0,0 +1,74 @@
+var oauthModule = require('./oauth2')
+ , querystring= require('querystring')
+ , request = require('request');
+
+var weibo = module.exports =
+oauthModule.submodule('weibo')
+ .configurable({
+ scope: "There's no idea about weibo's scope"
+ })
+
+ //fetch weibo user needs userid in extra
+ .step('fetchOAuthUser')
+ .accepts('accessToken extra')
+ .promises('oauthUser')
+
+ .oauthHost('https://api.weibo.com')
+ .apiHost('https://api.weibo.com')
+
+ .authPath('/oauth2/authorize')
+ .authQueryParam('response_type', 'code')
+
+ .accessTokenPath('/oauth2/access_token')
+ .accessTokenParam('grant_type', '')
+ .postAccessTokenParamsVia('data')
+
+ .entryPath('/auth/weibo')
+ .callbackPath('/auth/weibo/callback')
+
+ .authQueryParam('scope', function () {
+ return this._scope && this.scope();
+ })
+
+ .getAccessToken( function (code) {
+ var p = this.Promise()
+ , url = this._oauthHost + this._accessTokenPath
+ , opts = { url: url };
+
+ opts.form = {
+ client_id: this._appId
+ , redirect_uri: this._myHostname + this._callbackPath
+ , code: code
+ , client_secret: this._appSecret
+ };
+
+ request.post( opts, function(err, res, body){
+ var data;
+ if (err) {
+ p.fail(err);
+ } else {
+ data = JSON.parse(body); // sina weibo return a JSON with text/plain
+ p.fulfill(data.access_token, data);
+ delete data.access_token;
+ }
+ });
+ return p;
+ })
+ .fetchOAuthUser( function (accessToken, extra) {
+ var p = this.Promise();
+ var uid = extra.uid;
+ var url = this.apiHost() + "/2/users/show.json?uid=" + uid;
+
+ this.oauth.get(url, accessToken, function (err, user) {
+ if (err) {
+ p.fail(err);
+ }else{
+ p.fulfill(JSON.parse(user));
+ }
+ });
+
+ return p;
+ })
+ .convertErr( function (err) {
+ return new Error(err.data ? err.data : err);
+ });
Something went wrong with that request. Please try again.