Pergunta

I'm trying to figure out how I can add additional information from a user's Twitter account to the created account on a Meteor installation.

In particular I am trying to access the user's bio via Twitter Api v 1.1 and am not successful in doing so.

Therefore I am trying to extend Accounts.onCreateUser(function(options,user) {}); with the Twitter bio. How do I do that? And then access this data from a template?

Here's a perfect answer for returning data from Github, however I've had trouble porting this approach over to Twitter as the authenticating service: Meteor login with external service: how to get profile information?

Foi útil?

Solução

You could do it on this way:

Accounts.onCreateUser(function (options, user){
   user.profile = options.profile || {};
   //Twitter returns some useful info as the username and the picture
   if(user.services.twitter){
       user.profile.picture= user.services.twitter.profile_image_url_https;
       user.profile.username= user.services.twitter.screenName;
   }
   return user;
});

For getting the data from the Twitter API I´m using the node package oauth:

OAuth = Npm.require('oauth');
oauth = new OAuth.OAuth(
    'https://api.twitter.com/oauth/request_token',
    'https://api.twitter.com/oauth/access_token',
    'consumerKey',
    'secretKey',
    '1.0A',
    null,
    'HMAC-SHA1'
);
getTwitterUserData: function (id) {
    var accountUser = AccountsUserCollection.findOne({_id: id});
    var url = "https://api.twitter.com/1.1/users/show.json?screen_name="+accountUser.screen_name;
    oauth.get(url, 'accessToken', 'accessSecret', function (err, data, response) {
       if(err){
          console.log(err);
       }
       if(data){
           Fiber(function () {
               AccountsUserCollection.update({_id: accountUser._id}, {$set: {dataTwitter:    JSON.parse(data)}});
           }).run();
       }
       if(response){
           Log.info(response);
       }
    });
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top