令牌Google OAuth2.0的Exchange授权URL

时间:2019-06-05 03:59:08

标签: python django oauth-2.0 youtube-data-api google-oauth2

获得授权URL后,无法将其交换为令牌。每次致电(authorization_response=self.request)时,都会被AttributeError: 'WSGIRequest' object has no attribute 'lower'

击中

如何将身份验证URL交换为令牌?下面的代码:

urlpatterns = [
    path('ytauth/<int:pk>/', YtAuthView.as_view(), name='yt_auth'),
    path('oauth2callback/', Oauth2CallbackView.as_view(), name='oauth2callback')

]
class YtAuthView(UserPassesTestMixin, UpdateView):
    model = Profile
    form_class = YtAuthForm
    template_name = 'yt_auth_update.html'

    def form_valid(self, form):
        CLIENT_SECRETS_FILE = "collab-web-client.json"
        SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
        API_SERVICE_NAME = 'youtube'
        API_VERSION = 'v3'

        flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
            client_secrets_file=CLIENT_SECRETS_FILE,
            scopes=SCOPES)

        flow.redirect_uri = 'https://127.0.0.1:8000/profiles/oauth2callback/'

        auth_flow = youtube_base()
        auth_url = auth_flow.get_authenticated_service()

        return redirect(auth_url)

class youtube_base:

    def get_authenticated_service(self):

        flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
            client_secrets_file=CLIENT_SECRETS_FILE,
            scopes=SCOPES)

        flow.redirect_uri = 'http://127.0.0.1:8000/profiles/oauth2callback/'

        authorization_url, state = flow.authorization_url(access_type='offline')

        return authorization_url

这是我的授权URL的样子。

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=clientIdhere.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A8000%2Fprofiles%2Foauth2callback%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.force-ssl&state=t14XG3dAK0zQX8DOBGB0nO3Mk7nYes&access_type=offline
class Oauth2CallbackView(View):

    def get(self, request, *args, **kwargs):
        flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
            CLIENT_SECRETS_FILE, scopes=SCOPES)
        flow.redirect_uri = 'https://127.0.0.1:8000/profiles/oauth2callback/'


        flow.fetch_token(authorization_response=self.request)

        return redirect('http://127.0.0.1:8000/profiles/')

跟踪:

[05/Jun/2019 03:55:52] "POST /profiles/ytauth/29/ HTTP/1.1" 302 0
Internal Server Error: /profiles/oauth2callback/
Traceback (most recent call last):
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/michaelninh/PycharmProjects/Collab/profiles/views.py", line 109, in get
    flow.fetch_token(authorization_response=self.request)
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/google_auth_oauthlib/flow.py", line 241, in fetch_token
    self.client_config['token_uri'], **kwargs)
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/requests_oauthlib/oauth2_session.py", line 208, in fetch_token
    state=self._state)
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/clients/web_application.py", line 203, in parse_request_uri_response
    response = parse_authorization_code_response(uri, state=state)
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 261, in parse_authorization_code_response
    if not is_secure_transport(uri):
  File "/Users/michaelninh/.local/share/virtualenvs/Collab--WeU7_dP/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/utils.py", line 94, in is_secure_transport
    return uri.lower().startswith('https://')
AttributeError: 'WSGIRequest' object has no attribute 'lower'

2 个答案:

答案 0 :(得分:0)

您的声明:

  

flow.fetch_token(authorization_response = self.request)

不正确。使用authorization_response = self.request.url代替authorization_response = self.request

答案 1 :(得分:0)

self.request.get

<QueryDict: 
{'state': ['STATE'], 
'code': ['CODE'], 
'scope': ['SCOPE']}>

解决方案是flow.fetch_token(code=self.request.GET.get('code'))