Trying to use Google OAuth2 with requests - oauthlib I already have to use Ajax and the stuff But tried to log on to the Django backend with Google OAuth2 data, it seems that I would have to use a libary like this (because they are ready to work on middleware and an authentication backend, I write that content Do not want to)
I have another problem There I use the class-based ideas do, but tutorials are showing only function based views.
Okay, after getting my code from Google I am trying to bring an access token. The problem is that I can not get it to work.
This is where magic happens (not really .. actually):
Class log view (redirect view): authorization_base_url = r'https: //accounts.google.com / o / oauth2 / auth 'token_url = r'https: //accounts.google.com/o/oauth2/token' client_id = R 'was completely forgotten that a' client_secret = r'super, super secret 'redirect_uri =' http: // localhost: 8000 / day / callback 'scope = [' r'email ', r'https: // Www.googleapis.com/auth/userinfo.profile '] Get DEF (self, request): google = OAuth2 session (s) Own .client_id, redirect_uri = self.redirect_uri, scope = self.scope) authorization_url, state = google.authorization_url (self.authorization_base_url, access_type = 'offline', approval_prompt = "power") request.session ['oauth_state'] = state Return redirect (authorization _url)
This works just fine. And as soon as it comes back from Google:
Class callback view: Get Def (self, request): r = request.GET.copy () google = OAuth2 Session (login view) client_id, state = request.session ['oauth_state']) authorization_response = (LoginView.redirect_uri + "? / Callback state =" + request.session ['oauth_state'] + "& code =" + R ['code']) Tokens = google.fetch_token (LoginView.token_url, authorization_response = authorization_response, client_secret = LoginView.client_secret, redirect_uri = LoginView.redirect_uri,) ...
And of course My Traceback:
Traceback: File "/ home /patrik/.virtualenvs/cms/local/lib/pyth 112 of the on2.7 / site-packages / django / core / handlers / base.py "get_respon" response = wrapped_callback (request, * callback_pass, ** callback_keyware) file "/home/patrik/.virtualenvs/cms/local/ Lib / python2.7 / site-packages / django / views / generic / base.py "69 return file in self.dispatch (requests, * args, ** kwargs)" /home/patrik/.virtualenvs/cms/local /lib/python2.7/site-packages/django/views/generic/base Received "send 87" in the return handler (Request, * args, ** kwargs) file "/home/patrik/Schreibtisch/cms/cmsproject/apps/topics/views.py" 106 redirect_uri = LoginView.redirect_uri, on file /D / callback writing error: "/home/patrik/.virtualenvs/cms/local/lib/python2.7/site-packages/requests_oauthlib/oauth2_session.py" fetch_token 167. password = password, ** kwargs) Exception type Exception value in: ready_request_body () has received several values for keyword argument 'redirect_uri'
Where does this error come from?
fetch_token ()
method is not a redirect_uri Instead,
parameter will instead make this parameter available for example with the attribute google.redirect_uri
.
In other words, it should be used for OAuth2Session
Give it to the constructor instead:
google = OAuth2 session (loginview.client_id, state = request. Session ['oauth_state'], redirect_uri = LoginView.redirect_uri)
No comments:
Post a Comment