0

I'm doing a project using Meteor. People can log in with Twitter. I was wondering if there was a way to get someones profile picture from Twitter in the Accounts.onCreateUser. Here's what I have in mine :

Accounts.onCreateUser(function(options, user) {

   var twitterInfo =  user.services.twitter;

   if (options.profile){

        options.profile.createdAt = new Date();//now!
        options.profile.twitterId = twitterInfo.id;
        options.profile.username = twitterInfo.screenName;
        options.profile.name  = options.profile.name;
        user.profile = options.profile; 
   }

    return user;
}); 

Thanks!

moni
  • 201
  • 5
  • 14

4 Answers4

6

with twitter api 1.0 deprecation. the accepted answer is no longer valid.

Meteor.user().services.twitter.profile_image_url 

worked for me.

pahan
  • 2,445
  • 1
  • 28
  • 36
1

I've added the Meteor packages accounts-ui and accounts-twitter to my app. http://docs.meteor.com/#accountsui These packages add Twitter OAuth login capability really easily.

Better still: I can access the current user via Meteor.user() from anywhere. And even better, they come they're own templates. So I can just use {{services.twitter.screenName}} anywhere in my app to get the users twitter handle. {{services.twitter.profile_image_url}} for the avatar image url. And {{profile.name}} for the Twitter username.

weiphi
  • 161
  • 1
  • 3
0

As per the API (https://dev.twitter.com/docs/api/1/get/users/profile_image/%3Ascreen_name) add this in:

options.profile.display_picture="http://api.twitter.com/1/users/profile_image/"+twitterInfo.screenName;

This was the traditional way, in api 1.1 its slightly harder (https://dev.twitter.com/docs/user-profile-images-and-banners):

You need to get GET users/show and parse the user object for profile_image_url

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • 1
    api 1.0 is deprecated and no longer available. – pahan Sep 23 '13 at 05:55
  • I have also mentioned how to use 1.1, you need to get `/users/show` and get out `profile_image_url` from the user object. You need to OAuth first and use the token to make the request – Tarang Sep 23 '13 at 06:59
0

When a user logs in with twitter URL's for the profile pic is being set in the Users collection at services.twitter.profile_image_url and services.twitter.profile_image_url_https so you could just read out those URL's.

I use the following line to set a users twitter profile pic in the profile object, which is visible to the client by default:

Meteor.users.update({_id:Meteor.userId()}, {$set {"profile.twitterpic":user.services.twitter.profile_image_url}});
Martin
  • 548
  • 5
  • 14