简体   繁体   English

在 Django 视图中存储用户访问令牌

[英]Storing a users access token in django view

I am tryig to store a shopify access token in a django view upon first installation/auth (before they create an account & connect to DB).我试图在第一次安装/身份验证时(在他们创建帐户并连接到数据库之前)在 django 视图中存储一个 shopify 访问令牌。

I am new to django so please be nice.我是 Django 的新手,所以请保持友善。

How do I store this access token for usage later on?如何存储此访问令牌以供稍后使用? Can it be stored in "session"?它可以存储在“会话”中吗? is it already?是不是已经

Here's my repo and python view:这是我的repo和 python 视图:

def finalize(request):
    shop_url = request.GET['shop']
    auth_code = request.GET['code']
    hashed = request.GET['hmac']
    ts = request.GET['timestamp']
    print("shopURL", shop_url)

    print("success request")
    try:
        r = requests.post('https://'+shop_url+'/admin/oauth/access_token', data = {'client_id':'xx','client_secret':'xx','code':auth_code})

        print("request response > > > > ", r.json())
        this_response = r.json()
        print(this_response["access_token"],"this_response[access_token]")
       # >>>>>> STORE THIS TOKEN SOMEWHERE?
        request.session['shopify'] = {
            "shop_url": shop_url,
            "access_token": this_response["access_token"]
        }

    except Exception:
        messages.error(request, "Could not log in to Shopify store.")

        return redirect(reverse('shopify_app_login'))

    messages.info(request, "Logged in to shopify store.")

    response = redirect(_return_address(request))
    request.session.pop('return_to', None)
    return response

Best options is to store this in Django user.最好的选择是将其存储在 Django 用户中。 You can extend the Django user to add your own fileds您可以扩展 Django 用户以添加您自己的文件

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    shopify_access_token = models.CharField(max_length=200)

later you can access this token using request.user.profile.shopify_access_token稍后您可以使用request.user.profile.shopify_access_token访问此令牌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM