【问题标题】:oauth 1 not working with httpx and authliboauth 1 不适用于 httpx 和 authlib
【发布时间】:2022-11-07 19:20:23
【问题描述】:

我目前正在使用requests-oauthlib 库发出 oauth 1 签名请求。

from requests_oauthlib import OAuth1Session

self.session = OAuth1Session(
    OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
    oauth_token, oauth_token_secret,
    signature_type='auth_header', realm='http://api.twitter.com'
)       
self.session.headers = self.default_headers
self.session.verify = self.verify
self.session.proxies.update(self.proxies)

使用它我可以成功发出 oauth 1 请求。但我需要 http 2 并且想使用异步 io。这就是我尝试切换到httpxauthlib 的原因。

from authlib.integrations.httpx_client import OAuth1Client

self.session = OAuth1Client(
    OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
    oauth_token, oauth_token_secret,
    http2=True,
    headers=self.default_headers,
    proxies=self.proxies,
    verify=self.context
)
self.session.auth.realm = 'http://api.twitter.com'

使用requests-oauthlib,我可以毫无问题地发出签名请求。但是当我尝试对httpx 做同样的事情时,我得到了这样的回应:

{
    "errors": [{
        "code": 32,
        "message": "Could not authenticate you."
    }]
}

如果我使用 Web 调试器查看,我可以验证身份验证标头是否获得了所有正确的密钥,但是在使用 httpx 时请求会以某种方式失败。有人对如何解决此问题或如何正确调试有任何建议吗?提前致谢 :)

【问题讨论】:

    标签: python python-requests oauth authlib httpx


    【解决方案1】:

    我正在研究类似的解决方案,您应该在使用 httpx 时导入 AsyncOAuth1Client 而不是 OAuth1Client。

    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 2020-01-31
    • 2020-03-23
    • 2015-04-24
    • 1970-01-01
    相关资源
    最近更新 更多