在 Django 中编写两条腿的 OAuth 提供程序

发布于 2024-11-27 14:43:29 字数 102 浏览 3 评论 0原文

我正在寻找有关在 Django 中为 OAuth 编写两条腿的提供程序的教程/示例/说明。

很难找到有关 OAuth 提供程序的文档,更难找到有关两条腿系统的文档......

I'm looking for a tutorial/example/explanation about writing a two-legged provider for OAuth in Django.

It's hard to find documentation about a OAuth provider, and even harder about a two-legged system...

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

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

发布评论

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

评论(3

养猫人 2024-12-04 14:43:29

我花了大约 3 天的时间试图解决这个问题,并希望为任何可以使用它的人提供这个我最终从我试图查询的服务中获得的工作示例。结果非常容易。 PS 仅仅因为有人使用 oauth 1.0 并不就意味着您不能使用 oauth2 库。

要获取 auth2,请输入 pip install oauth2。

在您的脚本中,您需要:

import oauth2
import time
import urllib2


def build_request(url, method='GET'):
    params = {                                            
        'oauth_version': "1.0",
        'oauth_nonce': oauth2.generate_nonce(),
        'oauth_timestamp': int(time.time())
    }
    consumer = oauth2.Consumer(key='python_test',secret='your_secret')
    params['oauth_consumer_key'] = consumer.key

    req = oauth2.Request(method=method, url=url, parameters=params)
    signature_method = oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, None)
    return req

调用该函数并查看输出如下所示:

request = build_request('http://demo.echo360.com/ess/scheduleapi/v1/terms')
u = urllib2.urlopen(request.to_url())
print u.readlines()

I spent about 3 days trying to figure this out and wanted to provide anyone who can use it with this working example I finally got from the service I was trying to query. It wound up being extremely easy. P.S. Just because someone is using oauth 1.0 doesn't mean that you can't use the oauth2 library.

To get auth2, type pip install oauth2.

In your script, you need:

import oauth2
import time
import urllib2


def build_request(url, method='GET'):
    params = {                                            
        'oauth_version': "1.0",
        'oauth_nonce': oauth2.generate_nonce(),
        'oauth_timestamp': int(time.time())
    }
    consumer = oauth2.Consumer(key='python_test',secret='your_secret')
    params['oauth_consumer_key'] = consumer.key

    req = oauth2.Request(method=method, url=url, parameters=params)
    signature_method = oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, None)
    return req

Calling the function and viewing the output looks like this:

request = build_request('http://demo.echo360.com/ess/scheduleapi/v1/terms')
u = urllib2.urlopen(request.to_url())
print u.readlines()
仅此而已 2024-12-04 14:43:29

“2legged”只是普通的 OAuth 请求,没有访问令牌或访问令牌机密。就是这样。您仍然使用客户端凭据(标识符和密钥),但使用空字符串作为访问令牌参数。根据您使用的服务器库,您可以在发出请求时省略 oauth_token 参数。

'2 legged' is just normal OAuth request without an access token or access token secret. That's it. You still use the client credentials (identifier and secret) but use empty strings for the access token parameters. Depending on the server library you use, you can omit the oauth_token parameter when making the request.

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