python,tweepy:urllib2.HTTPError:HTTP错误403:禁止

python,tweepy:urllib2.HTTPError:HTTP错误403:禁止,python,python-2.7,twitter,tweepy,http-error,Python,Python 2.7,Twitter,Tweepy,Http Error,我正在编写一个Twitter应用程序,并试图使用tweepy进行授权。 我遇到了以下错误,无法找出原因 谁能帮我一下吗?我将不胜感激 Traceback (most recent call last): File "getconv.py", line 32, in <module> auth=AppAuthHandler(consumer_token,consumer_secret) File "getconv.py", line 25, in __init__ response=u

我正在编写一个Twitter应用程序,并试图使用tweepy进行授权。 我遇到了以下错误,无法找出原因

谁能帮我一下吗?我将不胜感激

Traceback (most recent call last):
File "getconv.py", line 32, in <module>
auth=AppAuthHandler(consumer_token,consumer_secret)
File "getconv.py", line 25, in __init__
response=urllib2.urlopen(req,data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 432, in error
result = self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 619, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden

该错误与tweepy无关,它来自您的自定义
AppAuthHandler

>>> auth = AppAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
演示:

>>AppAuthHandler(TWITTER\u消费者密钥,TWITTER\u消费者密钥)

您的问题标题有点误导,因为(假设您的身份验证信息是正确的)这似乎是tweepy的问题。另一方面,您的头意味着使用urllib2时出现问题directly@AndreasKlebinger我现在还不能确定错误是来自urllib2还是tweepy。不管怎样,我在标题上加了tweepy。非常感谢你的回答。现在,在您的帮助下,代码本身可以工作,但我仍然得到错误的身份验证数据:代码215错误。。身份验证后,我将通过url=“然后tweetdata=urlib2.urlopen(url).read()调用url。您能帮我找出原因吗?@CosmicRabbitMediaInc出于好奇,为什么不
api=tweepy.api(auth)
api.
?以及什么端点
”api.twitter.com/1.1/search/show.json?id=
你认为是吗?是1.0版的链接吗?我不知道你说的是什么意思。。上面url的结果只是“{”错误“:[{”消息“:“错误的身份验证数据”,“代码“:215}]}”,没有对1.0的引用
>>> auth = AppAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
class AppAuthHandler(tweepy.auth.AuthHandler):
    TOKEN_URL='https://api.twitter.com/oauth2/token'
    def __init__(self,consumer_key,consumer_secret):
        token_credential = '{}:{}'.format(*map(urllib.quote, [consumer_key, consumer_secret]))
        credential = base64.b64encode(token_credential)
        value = {'grant_type': 'client_credentials'}
        data = urllib.urlencode(value)
        req = urllib2.Request(self.TOKEN_URL)
        req.add_header('Authorization', 'Basic {}'.format(credential))
        req.add_header('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
        response=urllib2.urlopen(req, data)
        json_response=json.loads(response.read())
        self._access_token=json_response['access_token']
    def apply_auth(self,url,method,headers,parameters):
        headers['Authorization'] = 'Bearer {}'.format(self._access_token)
>>> AppAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
<__main__.AppAuthHandler object at 0x11b7d10>