Skip to content

Commit

Permalink
add flickr, douban oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
yyfrankyy committed Sep 10, 2012
1 parent 8e07871 commit 3de09a4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
60 changes: 60 additions & 0 deletions lib/modules/douban.js
Original file line number Original file line Diff line number Diff line change
@@ -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');
}
});
22 changes: 22 additions & 0 deletions lib/modules/flickr.js
Original file line number Original file line Diff line number Diff line change
@@ -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;
});
6 changes: 5 additions & 1 deletion lib/modules/oauth2.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ everyModule.submodule('oauth2')
} else if (resType.substring(0, 16) === 'application/json') { } else if (resType.substring(0, 16) === 'application/json') {
data = JSON.parse(body); data = JSON.parse(body);
} else { } 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; var aToken = data.access_token;
delete data.access_token; delete data.access_token;
Expand Down

0 comments on commit 3de09a4

Please sign in to comment.