“代码”:32,“消息”:“无法验证您的身份。”使用从tweepy获得的访问令牌时

问题描述 投票:0回答:1

我正在尝试使用Twitter的块上传功能。

参考:https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-init

由于该功能在tweepy中不可用,因此我试图从头开始实现。但是,由于用户已经使用tweepy登录到应用程序,因此tweepy保存了访问令牌和在该处获取的用户密码。

我尝试如下使用这些访问令牌,但出现错误"code":32,"message":"Could not authenticate you."

我想探索一种不会要求用户再次输入电子邮件和密码来上传视频的方法。

from requests_oauthlib import OAuth1, OAuth1Session
import os

def upload_video(video_file):
  video_file = os.path.join(MEDIA_ROOT, 'test_video.mp4')

  oauth_token = OAUTH_TOKEN
  oauth_token_secret = OAUTH_TOKEN_SECRET

  consumer_key = TWITTER_CONSUMER_TOKEN
  consumer_secret = TWITTER_CONSUMER_TOKEN_SECRET

  oauth = OAuth1Session(consumer_key,
                client_secret=consumer_secret,
                resource_owner_key=oauth_token,
                resource_owner_secret=oauth_token_secret,
                signature_method='HMAC-SHA1')

  headers = requests.utils.default_headers()

  headers.update(
    {
        'User-Agent': 'OAuth gem v0.4.4',
    }
  )

  video_url = 'https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes={size}&media_type={type}'.format(size=os.path.getsize(video_file), type='video/mp4')

  response = oauth.post(video_url,
                         headers=headers,
                         files=dict(foo='bar')) # to make it in multipart/form-data
  print(response.content)
  print(response.request.body)
  print(response.request.headers)  

我还打印了请求标头,如下所示:

{'User-Agent': b'OAuth gem v0.4.4', 'Accept-Encoding': b'gzip, deflate', 'Accept': b'*/*', 'Connection': b'keep-alive', 'Content-Length': '141', 'Content-Type': b'multipart/form-data; boundary=f5c7d61e8ab8a14b2a22ced4171b723e', 
'Authorization': b'OAuth oauth_nonce="147920959083366377161589749583", oauth_timestamp="1589749583", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="consumer_key", oauth_token="oauth_token", oauth_signature="mcEaLBsANVu%2B7lavaNfrOiHZbgs%3D"'}

[我也尝试过twurl命令,但确实得到了正确的响应,但是它要求我键入用户名和密码,并且生成了与tweepy不同的oauth_token和secret集。

python twitter oauth tweepy twurl
1个回答
0
投票

事实证明,我得到这个是因为我需要将参数放入数据中,而不是像查询一样。

此存储库确实对我如何将视频上传到Twitter有所帮助。

https://github.com/twitterdev/large-video-upload-python

© www.soinside.com 2019 - 2024. All rights reserved.