New package: accounts-weibo for Sina Weibo

This commit is contained in:
ZHANG Cheng
2012-06-27 16:52:24 +08:00
committed by Nick Martin
parent a0f564f9ef
commit d160097b81
4 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
Package.describe({
summary: "Login service for Sina Weibo accounts"
});
Package.on_use(function(api) {
api.use('accounts', ['client', 'server']);
api.use('accounts-oauth2-helper', ['client', 'server']);
api.use('http', ['client', 'server']);
api.add_files('weibo_common.js', ['client', 'server']);
api.add_files('weibo_server.js', 'server');
api.add_files('weibo_client.js', 'client');
});

View File

@@ -0,0 +1,18 @@
(function () {
Meteor.loginWithWeibo = function () {
if (!Meteor.accounts.weibo._clientId || !Meteor.accounts.weibo._appUrl)
throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts.weibo.config first");
var state = Meteor.uuid();
// XXX need to support configuring access_type and scope
var loginUrl =
'https://api.weibo.com/oauth2/authorize' +
'?response_type=code' +
'&client_id=' + Meteor.accounts.weibo._clientId +
'&redirect_uri=' + Meteor.accounts.weibo._appUrl + '/_oauth/weibo?close' +
'&state=' + state;
Meteor.accounts.oauth2.initiateLogin(state, loginUrl);
};
}) ();

View File

@@ -0,0 +1,8 @@
if (!Meteor.accounts.weibo) {
Meteor.accounts.weibo = {};
}
Meteor.accounts.weibo.config = function(clientId, appUrl) {
Meteor.accounts.weibo._clientId = clientId;
Meteor.accounts.weibo._appUrl = appUrl;
};

View File

@@ -0,0 +1,56 @@
(function () {
Meteor.accounts.weibo.setSecret = function (secret) {
Meteor.accounts.weibo._secret = secret;
};
Meteor.accounts.oauth2.registerService('weibo', function(query) {
if (query.error) {
// The user didn't authorize access
return null;
}
if (!Meteor.accounts.weibo._clientId || !Meteor.accounts.weibo._appUrl)
throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts.weibo.config first");
if (!Meteor.accounts.weibo._secret)
throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts.weibo.setSecret first");
var result = getAccessToken(query);
var identity = getIdentity(result.access_token, parseInt(result.uid, 10));
return {
userData: {name: identity.name, screen_name: identity.screen_name},
serviceUserId: result.uid,
serviceData: {accessToken: result.access_token}
};
});
var getAccessToken = function (query) {
var result = Meteor.http.post(
"https://api.weibo.com/oauth2/access_token", {params: {
code: query.code,
client_id: Meteor.accounts.weibo._clientId,
client_secret: Meteor.accounts.weibo._secret,
redirect_uri: Meteor.accounts.weibo._appUrl + "/_oauth/weibo?close",
grant_type: 'authorization_code'
}});
if (result.error) // if the http response was an error
throw result.error;
if (typeof result.content === "string")
result.content = JSON.parse(result.content);
if (result.content.error) // if the http response was a json object with an error attribute
throw result.content;
return result.content;
};
var getIdentity = function (accessToken, userId) {
var result = Meteor.http.get(
"https://api.weibo.com/2/users/show.json",
{params: {access_token: accessToken, uid: userId}});
if (result.error)
throw result.error;
return result.data;
};
})();