Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中带有urllib2的Twitter API_Python_Twitter - Fatal编程技术网

python中带有urllib2的Twitter API

python中带有urllib2的Twitter API,python,twitter,Python,Twitter,我想在Python中使用TwitterAPI,使用lookup方法从name中查找用户ID。我也曾简单地使用 response = urllib2.urlopen('http://search.twitter.com...') 但这一次我需要认证。我不认为我可以通过GooglePythonTwitterAPI做到这一点,因为它没有查找方法。有没有关于如何使用urllib2进行身份验证的想法???您最好使用一个用于Twitter API的实际Python库: 用于定义完整的HTTP标头: re

我想在Python中使用TwitterAPI,使用lookup方法从name中查找用户ID。我也曾简单地使用

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

但这一次我需要认证。我不认为我可以通过GooglePythonTwitterAPI做到这一点,因为它没有查找方法。有没有关于如何使用urllib2进行身份验证的想法???

您最好使用一个用于Twitter API的实际Python库:

用于定义完整的HTTP标头:

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

但是请注意,twitter上的基本授权将很快被禁用,您需要迁移到OAuth。有一些例子。

我建议使用tweepy API

此处的示例:

使用库,这是按名称查找用户ID的代码:

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

我希望这会有所帮助。

可以使用各种Twitter API包装库进行身份验证。中列出了包装器API的完整列表

无论库是什么,都必须实现OAuth身份验证。这是使用的示例代码

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