mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
New package: accounts-weibo for Sina Weibo
This commit is contained in:
13
packages/accounts-weibo/package.js
Normal file
13
packages/accounts-weibo/package.js
Normal 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');
|
||||
});
|
||||
18
packages/accounts-weibo/weibo_client.js
Normal file
18
packages/accounts-weibo/weibo_client.js
Normal 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);
|
||||
};
|
||||
|
||||
}) ();
|
||||
8
packages/accounts-weibo/weibo_common.js
Normal file
8
packages/accounts-weibo/weibo_common.js
Normal 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;
|
||||
};
|
||||
56
packages/accounts-weibo/weibo_server.js
Normal file
56
packages/accounts-weibo/weibo_server.js
Normal 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;
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user