Twitter API with urllib2 in python

I want to use the Twitter API in Python to search for user IDs from a name using a search method. I made similar requests just using

response = urllib2.urlopen('http://search.twitter.com...') 

but for this I need authentication. I don't think I can do this through the google python twitter API, because it has no search method. Any ideas how I can do authorization using urllib2 ??

+3
source share
5 answers

You should probably use one of the real Python libraries for the Twitter API:

http://dev.twitter.com/pages/libraries#python

+4
source

Use urllib2.Requestto determine the full HTTP header:

request = urllib2.Request( 'http://twitter.com/...' )
request.add_header( 'Authorization', 'Basic ' + base64.b64encode( username + ':' + password ) )
response = urllib2.urlopen( request )

, , twitter, OAuth. Twitter API Wiki .

+3

Twython, :

from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET,OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
print twitter.show_user(screen_name=USER_NAME)["id"]

, .

+1

API Twitter. API- Twitter.

, OAuth. Python Twitter Tool.

from twitter import *

MY_TWITTER_CREDS = os.path.expanduser('~/.my_app_credentials')
if not os.path.exists(MY_TWITTER_CREDS):
    oauth_dance("My App Name", CONSUMER_KEY, CONSUMER_SECRET,
                MY_TWITTER_CREDS)

oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)

twitter = Twitter(auth=OAuth(
    oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))

# Now search with Twitter
rel = t.search.tweets(q='whatever')['statuses']
# Now do whatever you want with rel object
0

Source: https://habr.com/ru/post/1745436/


All Articles