2017-12-01 6 views
0

最初のインストール/認証時に、店舗アクセストークンをdjangoビューに保存するようにしています(&データベースに接続する前に)。ユーザーのアクセストークンをdjagoビューに保存する

私はdjangoを新しくしていますので、よかったら教えてください。

後で使用するためにこのアクセストークンを保存するにはどうすればよいですか?それは "セッション"に格納することはできますか?それはすでにですか?

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 
+0

を使用して、このトークンにアクセスすることができ、独自のfiledsを追加するには、Djangoのユーザに拡張することができますか? – kshikama

+0

複数のセッションを介して..しかし、私はサイトを再訪しても、毎回再認証する必要があります。何かが働いていない@kshikama – NewPieGuy

答えて

1

ベストオプションはDjangoのユーザーでこれを格納することです:

は、ここに私のrepoとPythonの眺めです。あなたがセッション間またはちょうど現在のセッションのアクセストークンを保存しますか

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

あとでrequest.user.profile.shopify_access_token

関連する問題