Neara
Neara

Reputation: 3781

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

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.

Upvotes: 3

Views: 5347

Answers (5)

Vang Lian
Vang Lian

Reputation: 11

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

Upvotes: 0

Mike Helmick
Mike Helmick

Reputation: 313

All Twitter API v1.1 endpoints require authentication.

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

Upvotes: 0

ignacio.munizaga
ignacio.munizaga

Reputation: 1603

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)

Upvotes: 5

Neara
Neara

Reputation: 3781

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.

Upvotes: 0

pypat
pypat

Reputation: 1116

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

Upvotes: -1

Related Questions