Django-allauth gives 403 forbidden on callback

I am creating a custom provider to get oauth2 tokens from another internal project, the process is almost fine, but I get an error 403 forbiddenwhen the browser returns my project with this URL:

http://localhost:8001/account/connect/login/callback/?state=Nngd5Gu3JnB4&code=Rqqg91oEwQKDsvSyzZ8Az5fEeHGaEe#_=_

here is view.py in my custom provider:

import requests
from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter, OAuth2LoginView, OAuth2CallbackView)
from .provider import ConnectProvider


class ConnectOAuth2Adapter(OAuth2Adapter):
    provider_id = ConnectProvider.id
    access_token_url = 'http://localhost:8000/o/token/'
    authorize_url = 'http://localhost:8000/o/authorize/'
    profile_url = 'http://localhost:8000/api/account/'

    def complete_login(self, request, app, token, **kwargs):
        _token = {'access_token':token.token}
        resp = requests.get(self.profile_url, params={'access_token': token.token, 'alt': 'json'})
        extra_data = resp.json()
        login = self.get_provider().sociallogin_from_response(request, extra_data)
        return login

oauth2_login = OAuth2LoginView.adapter_view(GoConnectOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(GoConnectOAuth2Adapter)
+3
source share
1 answer

After going through the source code allauth, I realized that the problem is with the session:

 @classmethod
  def stash_state(cls, request):  
      state = cls.state_from_request(request) 
      verifier = get_random_string()  
      request.session['socialaccount_state'] = (state, verifier)
      return verifier



@classmethod
  def verify_and_unstash_state(cls, request, verifier):
      if 'socialaccount_state' not in request.session:
          raise PermissionDenied()        
      state, verifier2 = request.session.pop('socialaccount_state')
      if verifier != verifier2:       
          raise PermissionDenied()        
      return state

2 -, - OAuth (localhost: 8000), - OAuth (localhost: 8001), stash_state, , , , .

+3

All Articles