【问题标题】:Storing a users access token in django view在 django 视图中存储用户访问令牌
【发布时间】:2017-12-01 06:19:48
【问题描述】:

我正在尝试在第一次安装/身份验证时将 shopify 访问令牌存储在 django 视图中(在他们创建帐户并连接到数据库之前)。

我是 django 的新手,所以请善待。

如何存储此访问令牌以供以后使用?它可以存储在“会话”中吗?已经了吗?

这是我的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

【问题讨论】:

  • 您要跨会话存储访问令牌还是仅存储当前会话?
  • 跨会话.. 但即使我重新访问该站点 - 我每次都必须重新验证.. 某些东西不起作用@kshikama

标签: python django shopify


【解决方案1】:

最好的选择是将其存储在 Django 用户中。您可以扩展 Django 用户以添加您自己的文件

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

稍后您可以使用 request.user.profile.shopify_access_token 访问此令牌

【讨论】:

    猜你喜欢
    • 2011-09-06
    • 2019-11-16
    • 2012-07-18
    • 2021-06-12
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多