Permalink
Browse files

add flickr, douban oauth

  • Loading branch information...
1 parent 8e07871 commit 3de09a4e73cda3dc5ba1c1cace06d9b7ec6db8fa @yyfrankyy yyfrankyy committed Sep 10, 2012
Showing with 87 additions and 1 deletion.
  1. +60 −0 lib/modules/douban.js
  2. +22 −0 lib/modules/flickr.js
  3. +5 −1 lib/modules/oauth2.js
View
@@ -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
@@ -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
@@ -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;

0 comments on commit 3de09a4

Please sign in to comment.