Skip to content

Loading…

Add flickr login and douban (Oauth 2.0) login #338

Merged
merged 4 commits into from

2 participants

@yyfrankyy

douban is for http://www.douban.com The most famous book, movie, music community in China, which just release their oauth 2.0 API at 2012-09-06

@bnoguchi bnoguchi merged commit 6d335c5 into bnoguchi:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 10, 2012
  1. @yyfrankyy

    add flickr, douban oauth

    yyfrankyy committed
  2. @yyfrankyy
Commits on Sep 23, 2012
  1. @yyfrankyy

    add taobao

    yyfrankyy committed
Commits on Sep 24, 2012
  1. @yyfrankyy

    make taobao login work

    yyfrankyy committed
Showing with 202 additions and 1 deletion.
  1. +61 −0 README.md
  2. +60 −0 lib/modules/douban.js
  3. +22 −0 lib/modules/flickr.js
  4. +5 −1 lib/modules/oauth2.js
  5. +54 −0 lib/modules/taobao.js
View
61 README.md
@@ -2453,6 +2453,67 @@ everyauth.dailycred
.redirectPath('/');
```
+### Flickr OAuth (1.0)
+
+You will first need to [request an API key](http://www.flickr.com/services/apps/create/apply) to get the consumer key and secret.
+
+```javascript
+var everyauth = require('everyauth')
+ , connect = require('connect');
+
+everyauth
+ .flickr
+ .consumerKey('YOUR CONSUMER KEY HERE')
+ .consumerSecret('YOUR CONSUMER SECRET HERE')
+ .findOrCreateUser( function (sess, accessToken, accessSecret, user) {
+ // find or create user logic goes here
+ })
+ .redirectPath('/');
+
+var routes = function (app) {
+ // Define your routes here
+};
+
+connect(
+ connect.bodyParser()
+ , connect.cookieParser()
+ , connect.session({secret: 'whodunnit'})
+ , everyauth.middleware()
+ , connect.router(routes);
+).listen(3000);
+```
+
+### Douban OAuth (2.0)
+
+You will first need to Take a look at [douban's developer wiki](http://developers.douban.com/wiki) to apply a AppKey and AppSecret.
+
+```javascript
+var everyauth = require('everyauth')
+ , connect = require('connect');
+
+everyauth
+ .douban
+ .appId('YOUR API Key')
+ .appSecret('YOUR API Secret')
+ .findOrCreateUser( function (sess, accessToken, accessSecret, user) {
+ // find or create user logic goes here
+ })
+ .scope('email') // Defaults to douban_basic_common
+ .redirectPath('/');
+
+var routes = function (app) {
+ // Define your routes here
+};
+
+connect(
+ connect.bodyParser()
+ , connect.cookieParser()
+ , connect.session({secret: 'whodunnit'})
+ , everyauth.middleware()
+ , connect.router(routes);
+).listen(3000);
+```
+
## Configuring a Module
everyauth was built with powerful configuration needs in mind.
View
60 lib/modules/douban.js
@@ -0,0 +1,60 @@
+var oauthModule = require('./oauth2')
+ , request = require('request');
+
+module.exports =
+oauthModule.submodule('douban')
+ .configurable({
+ scope: 'specify types of access: (no scope), douban_basic_common, shuo_basic_r, shuo_basic_w..'
+ })
+ .apiHost('https://api.douban.com/')
+
+ .oauthHost('https://www.douban.com')
+ .authPath('/service/auth2/auth')
+ .accessTokenPath('/service/auth2/token')
+
+ .authQueryParam('response_type', 'code')
+ .authQueryParam('scope', function () {
+ return this._scope && this.scope();
+ })
+
+ .accessTokenParam('grant_type', 'authorization_code')
+ .postAccessTokenParamsVia('data')
+
+ .entryPath('/auth/douban')
+ .callbackPath('/auth/douban/callback')
+
+ .fetchOAuthUser( function (accessToken) {
+ var p = this.Promise();
+ request.get({
+ url: this.apiHost() + '/v2/user/~me'
+ , headers: {
+ Authorization: 'Bearer ' + accessToken
+ }
+ }, function (err, res, data) {
+ if (err) return p.fail(err);
+ var oauthUser = JSON.parse(data);
+ p.fulfill(oauthUser);
+ });
+ return p;
+ })
+
+ .moduleErrback( function (err, seqValues) {
+ if (err instanceof Error) {
+ var next = seqValues.next;
+ return next(err);
+ } else if (err.extra) {
+ var ghResponse = err.extra.res
+ , serverResponse = seqValues.res;
+ serverResponse.writeHead(
+ ghResponse.statusCode
+ , ghResponse.headers);
+ serverResponse.end(err.extra.data);
+ } else if (err.statusCode) {
+ var serverResponse = seqValues.res;
+ serverResponse.writeHead(err.statusCode);
+ serverResponse.end(err.data);
+ } else {
+ console.error(err);
+ throw new Error('Unsupported error type');
+ }
+ });
View
22 lib/modules/flickr.js
@@ -0,0 +1,22 @@
+var oauthModule = require('./oauth');
+
+module.exports =
+oauthModule.submodule('flickr')
+ .apiHost('http://api.flickr.com/services/rest')
+ .oauthHost('http://www.flickr.com/services/oauth')
+ .entryPath('/auth/flickr')
+
+ .requestTokenPath('/request_token')
+ .accessTokenPath('/access_token')
+ .authorizePath('/authorize')
+
+ .callbackPath('/auth/flickr/callback')
+ .fetchOAuthUser( function (accessToken, accessTokenSecret, params) {
+ var p = this.Promise();
+ this.oauth.get(this.apiHost() + '?nojsoncallback=1&format=json&method=flickr.test.login', accessToken, accessTokenSecret, function (err, data) {
+ if (err) { return p.fail(err); }
+ var oauthUser = JSON.parse(data);
+ p.fulfill(oauthUser);
+ });
+ return p;
+ });
View
6 lib/modules/oauth2.js
@@ -197,7 +197,11 @@ everyModule.submodule('oauth2')
} else if (resType.substring(0, 16) === 'application/json') {
data = JSON.parse(body);
} else {
- throw new Error('Unsupported content-type ' + resType);
+ try {
+ data = JSON.parse(body);
+ } catch (e) {
+ throw new Error('Unsupported content-type ' + resType);
+ }
}
var aToken = data.access_token;
delete data.access_token;
View
54 lib/modules/taobao.js
@@ -0,0 +1,54 @@
+var oauthModule = require('./oauth2')
+ , request = require('request');
+
+module.exports =
+oauthModule.submodule('taobao')
+ .configurable({
+ scope: 'specify types of access: item,, promotion, usergrade'
+ })
+ .apiHost('https://open.taobao.com/')
+
+ .oauthHost('https://oauth.taobao.com')
+ .authPath('/authorize')
+ .accessTokenPath('/token')
+
+ .authQueryParam('response_type', 'code')
+ .authQueryParam('scope', function () {
+ return this._scope && this.scope();
+ })
+
+ .accessTokenParam('grant_type', 'authorization_code')
+ .postAccessTokenParamsVia('data')
+
+ .entryPath('/auth/taobao')
+ .callbackPath('/auth/tb/callback')
+
+ .fetchOAuthUser(function(accessToken, accessExtra, params) {
+ var extra = accessExtra.extra
+ return {
+ id: extra.taobao_user_id
+ , token: accessToken
+ , extra: extra
+ }
+ })
+
+ .moduleErrback(function(err, seqValues) {
+ if (err instanceof Error) {
+ var next = seqValues.next;
+ return next(err);
+ } else if (err.extra) {
+ var ghResponse = err.extra.res
+ , serverResponse = seqValues.res;
+ serverResponse.writeHead(
+ ghResponse.statusCode
+ , ghResponse.headers);
+ serverResponse.end(err.extra.data);
+ } else if (err.statusCode) {
+ var serverResponse = seqValues.res;
+ serverResponse.writeHead(err.statusCode);
+ serverResponse.end(err.data);
+ } else {
+ console.error(err);
+ throw new Error('Unsupported error type');
+ }
+ });
Something went wrong with that request. Please try again.