3

How to get user profile image using Twython?

I see show_user() method, but instantiating Twython with api key and secret + oauth token and secret, and calling this method returns 404: TwythonError: Twitter API returned a 404 (Not Found), Sorry, that page does not exist.

Calling same method from Twython instantiated w.o api/oauth keys returns 400: TwythonAuthError: Twitter API returned a 400 (Bad Request), Bad Authentication data.

I also tried to GET user info from https://api.twitter.com/1.1/users/show.json?screen_name=USERSCREENNAME, and got 400 as well.

I would appreciate a working example of authenticated request to twitter api 1.1 . Can't find it on twitter api reference.

Neara
  • 3,693
  • 7
  • 29
  • 40

5 Answers5

5

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)
ignacio.munizaga
  • 1,553
  • 1
  • 23
  • 28
  • yep, you are right. yesterday i used another endpoint (get_mentions_timeline) in same way. – Neara Jun 21 '13 at 08:42
0

I solved my issue, following way:

    api = 'https://api.twitter.com/1.1/users/show.json'
    args = {'screen_name': account_name}
    t = Twython(app_key=settings.TWITTER_CONSUMER_KEY,
                 app_secret=settings.TWITTER_CONSUMER_SECRET,
                 oauth_token=token.token,
                 oauth_token_secret=token.secret)
    resp = t.request(api, params=args)

this returns a json respons, see twitter docs. So in my case: resp['profile_image_url_https'] gives the url to user profile image in normal size for twitter, which is 48px by 48px.

Neara
  • 3,693
  • 7
  • 29
  • 40
0

All Twitter API v1.1 endpoints require authentication.

This example is correct: Twitter API/Twython - show user to get user profile image

Community
  • 1
  • 1
Mike Helmick
  • 313
  • 1
  • 5
0

Here is How i did it using twython for getting user details (Python 3). You can refer all the key id's of the Json here: https://dev.twitter.com/docs/api/1.1/get/users/show

from twython import Twython

APP_KEY = 'xxxx'
APP_SECRET = 'xxxxx'
OAUTH_TOKEN = 'xxxx'
OAUTH_TOKEN_SECRET = 'xxx'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

details = twitter.show_user(screen_name='lyanaz')
print (details['profile_image_url']) #Prints profile image URL
Vang Lian
  • 11
  • 1
-1

From the examples here : https://github.com/ryanmcgrath/twython/tree/master/examples

from twython import Twython

# Requires Authentication as of Twitter API v1.1
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

avatar = open('myImage.png', 'rb')
twitter.update_profile_image(image=avatar)

This actually changes the avatar but it should get you started.

Also here's how to properly authenticate: https://github.com/ryanmcgrath/twython#authorization-url

pypat
  • 1,096
  • 1
  • 9
  • 19
  • I dont want to update profile image, just to get the link to it. An i already have a proper authentication process. – Neara Jun 03 '13 at 12:55
  • You said you wanted "a working example of authenticated request to twitter api 1.1". That's what I gave you. – pypat Jun 03 '13 at 13:04
  • already authenticated, not how to authenticate a user, not oauth dance. how looks a request with all the tokens? how might look a request though urllib or requests lib to twitter api, for already authenticated user? – Neara Jun 03 '13 at 16:08