twitter开发人员api oauth提供了错误的身份验证数据,但一切都很好

问题描述

试图让Twitter验证我对新API的获取请求,以获取有关特定主题的最新推文。身份验证存在一些问题,似乎无法确定。我使用客户端密钥和客户端密钥对我的应用程序进行了身份验证,然后对用户进行了身份验证并接受了该应用程序可以读取和写入权限。使用用户身份验证令牌和密码,我尝试进行身份验证以从最新的api中获取数据,并收到错误的身份验证错误。

您能看到我在做什么吗?:

curl --request GET --url 'https://api.twitter.com/2/tweets/search/recent?query=python' --header \

'authorization: OAuth \
oauth_consumer_key="i_put_api_key_here",\
oauth_consumer_secret="i_put_api_secret_here",\
oauth_token="i_put_user_token_after_accepting_app_can_make_changes",\
oauth_token_secret="i_put_oauth_token_secret",\
oauth_signature_method="HMAC-SHA1",\
oauth_timestamp="",\
oauth_version="1.0"'

return data is:
{"title":"Unauthorized","type":"about:blank","status":401,"detail":"Unauthorized"}

我指的是这份文件:https://developer.twitter.com/en/docs/authentication/oauth-1-0a

我很确定我可以正确提供所需的所有数据。

解决方法

Python挽救了这一天。您可以从Twitter Api仪表板请求该应用程序的承载令牌。然后像这样使用OAuth 2.0,

def get_data(url):
    headers = {'Authorization': "Bearer " + str(TWITTER_BEARER_TOKEN) }
    response = requests.get(url,headers=headers)
    response_data = response.json()
    return response_data

get_data('https://api.twitter.com/2/tweets/search/recent?query="Python";max_results=10')
Out[20]: 
{'data': [{'id': '1294146621652045826','text': 'RT @jacoblawherlin: @Taco_Farmer1 Maybe this is why Python is so popular for data science but not software dev (in addition to performance)…'},

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...