OAuth 1不使用HTTPX和Authlib

发布于 2025-02-09 06:32:55 字数 1333 浏览 3 评论 0原文

我目前正在使用request-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'

使用request-oauthlib我可以毫无问题地提出签名请求。但是,当我尝试使用httpx做同样的事情时,我会得到此响应:

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

如果我使用网络调试器查看,我可以验证身份验证标头是否获得了所有正确的键HTTPX。有人对如何解决此问题或如何正确调试这个问题有什么建议吗?提前致谢 :)

i am currently making oauth 1 signed requests using the requests-oauthlib library.

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)

Using this i can successfully make oauth 1 requests. But I need http 2 and would like to use async io. Thats why i am trying to switch to httpx and authlib.

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'

With requests-oauthlib i can make signed requests without a problem. But when i try to do the same with httpx i get this response:

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

If i take a look with a web debugger i can verify that the authentication header got all the right keys but somehow the request fails while using httpx. Does anyone have any suggestions on how to resolve this issue or how to debug this properly? Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2025-02-16 06:32:55

我正在使用类似的解决方案,您应该在使用HTTPX时导入Asyncoauth1client而不是OAuth1client。

I'm working on a similar solution, you should import AsyncOAuth1Client and not OAuth1Client when using httpx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文