【问题标题】:oauth2 POST - twitteroauth2 发布 - 推特
【发布时间】:2023-03-12 21:35:02
【问题描述】:

我创建了一个脚本来获取用户好友列表(GET 请求),我成功了。现在我正在尝试制作一个将跟随特定用户(POST 请求)的脚本,但我没有成功。

这是我的 oauth 函数(问题所在):

def augment_POST(url,**kwargs) :
    secrets = hidden.oauth()
    consumer = oauth2.Consumer(secrets['consumer_key'], secrets['consumer_secret'])
    token = oauth2.Token(secrets['token_key'],secrets['token_secret'])

    oauth_request = oauth2.Request.from_consumer_and_token(consumer, token= token, http_method='POST', http_url=url, parameters=kwargs)
    oauth_request.to_postdata() # this returns post data, where should i put it?
    oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
    return oauth_request.to_url()

我的 augment_GET 函数除了 http_mehtod='GET' 之外完全相同

为了清楚起见:

def follow_user(id):
    seedurl="https://api.twitter.com/1.1/friendships/create.json"
    print 'Attempting to follow: %d' % (id,)
    url = augment_POST(seedurl,user_id=id)
    connection = urllib.urlopen(url)
    data = connection.read()
    headers = connection.info().dict

任何帮助将不胜感激。

【问题讨论】:

  • 它不应该使用 oauth2 来发出这个请求。这个 OAuth 库是什么?
  • 不要介意这个库有一个令人困惑的名字
  • 您的链接出现错误 404
  • documentation *抱歉。
  • 它不是你的库的文档,它是 OAuth2 协议(如果你想在 twitter 上关注用户,这不是你想要的)

标签: python twitter oauth-2.0 twitter-oauth


【解决方案1】:

首先,您似乎需要import urllib2 才能发出 POST 请求。
您必须发送从 to_postdata 方法获得的 POST 数据 使用urlopendata 参数:

def augment_POST(url, **kwargs) :
    secrets = hidden.oauth()
    consumer = oauth2.Consumer(secrets['consumer_key'],
                               secrets['consumer_secret'])
    token = oauth2.Token(secrets['token_key'],
                         secrets['token_secret'])

    oauth_request = oauth2.Request.from_consumer_and_token(
        consumer,
        token= token,
        http_method='POST',
        http_url=url,
        parameters=kwargs
    )

    oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(),
                               consumer, token)

    # this is the data that must be sent with you POST request
    return oauth_request.to_postdata()


def follow_user(id):
    url = "https://api.twitter.com/1.1/friendships/create.json"
    print 'Attempting to follow: %d' % id
    postdata = augment(url, method='GET', user_id=id)

    # Send the POST request with the data argument
    # The url is the same as the data is sent in the body of the request
    connection = urllib2.urlopen(url, data=postdata)

    data = connection.read()
    headers = connection.info().dict

我建议使用requests_oauthlib 模块,它让这一切变得非常简单:

from requests_oauthlib import OAuth1Session

tokens = hidden.oauth()
client = OAuth1Session(tokens['consumer_key'],
                       tokens['consumer_secret'],
                       tokens['token_key'],
                       tokens['token_secret'])


def follow_user(id):
    url = "https://api.twitter.com/1.1/friendships/create.json"
    print 'Attempting to follow: %d' % id

    # for GET requests use client.get and the `params` argument
    # instead of the `data` argument
    response = client.post(url, data={'user_id': id})

    data = response.text
    # or even `data = response.json()` to decode the data
    headers = response.headers

【讨论】:

  • 非常感谢!我可以就另一个话题联系你吗?从 twitter 获取不记名令牌。我理解这个概念,我只是不知道你是如何实现它的。
  • 要获取不记名令牌,您必须使用您的使用者密钥作为用户名和您的使用者密码作为密码来创建一个基本授权标头。然后使用您生成的标头向api.twitter.com/oauth2/token 发送 POST 请求。您可以使用 json 解析响应并获取您的承载。然后,您只需将您的不记名令牌以“不记名{bearer_token}”的格式放入您的身份验证令牌中。
猜你喜欢
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多