How to Get User Image with Twitter API 1.1

How to get user image with Twitter API 1.1?

The user's profile image

Okay, so you want a user's profile image. You're going to need to take a look at the twitter REST API 1.1 docs. This is a list of all the different requests you can make to their API (don't worry, I'll get to how you actually do this later on).

There are multiple ways to get the user's profile image, but the most notable one is: users/show. According to the docs for this, the users/show method:

Returns a variety of information about the user specified by the required user_id or screen_name parameter. The author's most recent Tweet will be returned inline when possible.

Well, the user profile image must be in there somewhere, correct?

Let's have a look at a typical response to a request for this information, using the users/show url (we'll use my profile as an example).

Example response for users/show from the twitter 1.1 api

I've cut off some from the bottom, because there is a lot of data to go through. Most importantly, you'll see what you require:

profile_image_url key

This is the profile_image_url key that you need to get access to.

So, how do you do all this? It's pretty simple, actually.

Authenticated Requests

As you rightly pointed out, as of June 11th 2013 you can't make unauthenticated requests, or any to the 1.0 API any more, because it has been retired. So OAuth is the way to make requests to the 1.1 API.

I wrote a stack overflow post with an aim to help all you guys make authenticated requests to the 1.1 API with little to no effort.

When you use it, you'll get back the response you see above. Follow the posts instructions, step-by-step, and you can get the library here (you only need to include one file in your project).

Basically, the previous post explains that you need to do the following:

  • Create a twitter developer account
  • Get yourself a set of unique keys from twitter (4 keys in total).
  • Set your application to have read/write access
  • Include TwitterApiExchange.php (the library)
  • Put your keys in a $settings array
  • Choose your URL and request method (Post/Get) from the docs (I put the link above!)
  • Make the request, that's it!

A practical example

I'm going to assume you followed the step-by-step instructions in the above post (containing pretty colour pictures). Here's the code you would use to get what you want.

// Require the library file, obviously
require_once('TwitterAPIExchange.php');

// Set up your settings with the keys you get from the dev site
$settings = array(
'oauth_access_token' => "YOUR_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);

// Chooose the url you want from the docs, this is the users/show
$url = 'https://api.twitter.com/1.1/users/show.json';
// The request method, according to the docs, is GET, not POST
$requestMethod = 'GET';

// Set up your get string, we're using my screen name here
$getfield = '?screen_name=j7mbo';

// Create the object
$twitter = new TwitterAPIExchange($settings);

// Make the request and get the response into the $json variable
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();

// It's json, so decode it into an array
$result = json_decode($json);

// Access the profile_image_url element in the array
echo $result->profile_image_url;

That's pretty much it! Very simple. There's also users/lookup which effectively does the same thing, but you can:

Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters.

If you ever need to get more than one user's details, use that, but as you only require one user's details, use users/show as above.

I hope that cleared things up a bit!

Twitter API 1.1 User Image and name

You can only get the user image and user name with authentication.
If you're trying to get the user profile image with Abraham's twitteroauth, then I guess this link will help you.
You can get the user name similarly by calling the name object and display it.

Get Twitter profile image with username or id with V1.1

Before we were able to access the the user's profile image without authentication using the twitter version 1 api.

Now that this has been deprecated, we much use /1.1/users/show.json to pull the profile image with authentication information.

Pull Twitter Profile Image in Twitter API VERSION 1.1

You can use as below, I am using Tweet Sharp:

While callback twitter is providing ProfileImageURL. Maintain this and again
hit the provided url using OAuth, it will return Twitter user image URL

OR using below URL, hit with OAuth and get ImageURL.

https://api.twitter.com/1.1/users/show.json?screen_name=amit

For non secure, it will return in ProfileImageUrl and for secure ProfileImageUrlHttps

Building Twitter profile image url with Twitter user id

With API 1.1 you can achieve this using these URLs:

  • https://twitter.com/[screen_name]/profile_image?size=mini
  • https://twitter.com/[screen_name]/profile_image?size=normal
  • https://twitter.com/[screen_name]/profile_image?size=bigger
  • https://twitter.com/[screen_name]/profile_image?size=original

Official twitter documentation Profile Images and Banners

Example

https://twitter.com/TwitterEng/profile_image?size=original

will redirect to

https://pbs.twimg.com/profile_images/875168599299637248/84CkAq6s.jpg

How to get all images uploaded by the user?

There's no specific media fetching endpoint in the Twitter API, so yes, you'd need to do something along those lines.

  • call the statuses/user_timeline.json endpoint and page through it (supports up to 3200 Tweets). I'd suggest using the ?tweet_mode=extended parameter, to ensure you get media entities for any longer Tweets
  • iterate through the Tweet objects, looking for any extended_entities objects (these contain images or videos)
  • grab the images from the URL inside the object

How to get twitter profile pic using api 1.1 from iOS?

Here is a solution using STTwitter:

STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"" consumerSecret:@""];

[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {

[twitter getUsersShowForUserID:nil orScreenName:@"barackobama" includeEntities:nil successBlock:^(NSDictionary *user) {

NSString *profileImageURLString = [user valueForKey:@"profile_image_url"];
NSURL *url = [NSURL URLWithString:profileImageURLString];
UIImage *profileImage = [UIImage imageWithContentsOfURL:url];

} errorBlock:^(NSError *error) {
//
}];

} errorBlock:^(NSError *error) {
//
}];

Twitter API/Twython - show user to get user profile image

You need to call the show_user method with the screen_name argument

t = Twython(app_key=settings.TWITTER_CONSUMER_KEY,
app_secret=settings.TWITTER_CONSUMER_SECRET,
oauth_token=oauth_token,
oauth_token_secret=oauth_token_secret)

print t.show_user(screen_name=account_name)


Related Topics



Leave a reply



Submit