Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Rauth和Django正确创建OAuth2Service_Python_Django_Rauth - Fatal编程技术网

Python 使用Rauth和Django正确创建OAuth2Service

Python 使用Rauth和Django正确创建OAuth2Service,python,django,rauth,Python,Django,Rauth,我正在使用rauth对stripe connect进行身份验证。为此,我需要实例化一个OAuth2Service,以便在多个视图中使用。现在,我的视图文件看起来很像这样(并且可以正常工作),但这感觉不对: from rauth.service import Oauth2Service service = OAuth2Service( name = 'stripe', client_id = 'my_client_id', client_secret = 'my_secr

我正在使用rauth对stripe connect进行身份验证。为此,我需要实例化一个OAuth2Service,以便在多个视图中使用。现在,我的视图文件看起来很像这样(并且可以正常工作),但这感觉不对:

from rauth.service import Oauth2Service

service = OAuth2Service(
    name = 'stripe',
    client_id = 'my_client_id',
    client_secret = 'my_secret',
    authorize_url = 'auth_url',
    access_token_url = 'stripe_access_token_url',
    base_url = 'stripe_api_url',
)

def stripe_auth(request):
    params = {'response_type': 'code'}
    url = service.get_authorize_url(**params)
    return HttpResponseRedirect(url)

def stripe_callback(request):
    code = request.GET['code']
    data = {
        'grant_type': 'authorization_code',
        'code': code
    }
    resp = service.get_raw_access_token(method='POST', data=data)
    ... rest of view code ...
我的问题是,我觉得将“service”变量放在视图之外是错误的,但我不确定该如何处理这个问题。我应该把它拆分成一个单独的模块,放在设置文件中,创建一个装饰器吗?我不是很确定


非常感谢任何建议。

我通常将其作为属性添加到Flask应用程序对象中,如下所示:

app = Flask(....)
app.stripe = OAuth2Service(
    name = 'stripe',
    client_id = 'my_client_id',
    client_secret = 'my_secret',
    authorize_url = 'auth_url',
    access_token_url = 'stripe_access_token_url',
    base_url = 'stripe_api_url',
)

这使得它很容易访问。

从Flask的角度来看,我不认为将服务变量放在视图之外是错误的。事实上,这就是我使用劳思的方式。服务变量当然可以存在于它自己的模块中,如果这让您感觉更好的话。@maxcountryman谢谢!我确实想过把它放在一个模块中,但它看起来确实有点像杀伤力过大。如果我最终在其他应用程序中使用它,我肯定会采取这一步,但我倾向于将它留在原来的位置,除非有人另有说明。再次感谢。