Skip to content

Twitter OAuth using access_token #272

@maryokhin

Description

@maryokhin

I am using Django REST Framework + Python-Social-Auth. I already got Facebook and Google OAuth2 working, but am having a problem using the same code to connect Twitter. I am having the same error as #107, but solution does not work because all work is done on the client and I have to work with keys in the request:

{
    "backend": "twitter",
    "access_token": "2478*************************************tdkiOW",
    "access_token_secret": "WgaYaEyHL******************************X6UpzAlTgITPa"
}
kwargs = {key: value for key, value in serializer.data.items() if key != 'backend'}
user = request.user
kwargs['user'] = user.is_authenticated() and user or None
user = strategy.backend.do_auth(**kwargs)

I also tried to do something like this:

if serializer.is_valid():
            backend = serializer.data['backend']
            oauth_token = serializer.data['access_token']
            oauth_token_secret = serializer.data['access_token_secret']
...
twitter = {'oauth_token': oauth_token, 'oauth_token_secret': oauth_token_secret}
user = strategy.backend.do_auth(twitter)

But no matter what I tried, I always got the same stacktrace

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/social-auth/

Django Version: 1.7b3
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'api',
 'rest_framework.authtoken',
 'social.apps.django_app.default')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  113.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
  400.             response = self.handle_exception(exc)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
  397.             response = handler(request, *args, **kwargs)
File "/Users/maryokhin/Workspace/backend/api/views/auth.py" in post
  54.           user = strategy.backend.do_auth(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/social/backends/oauth.py" in do_auth
  124.         data = self.user_data(access_token)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/social/backends/twitter.py" in user_data
  33.             auth=self.oauth_auth(access_token)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/social/backends/base.py" in get_json
  195.         return self.request(url, *args, **kwargs).json()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/social/backends/base.py" in request
  188.             response = request(method, url, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/api.py" in request
  44.     return session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py" in request
  349.         prep = self.prepare_request(req)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py" in prepare_request
  287.             hooks=merge_hooks(request.hooks, self.hooks),
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py" in prepare
  291.         self.prepare_auth(auth, url)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py" in prepare_auth
  470.             r = auth(self)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests_oauthlib/oauth1_auth.py" in __call__
  67.                 unicode(r.url), unicode(r.method), None, r.headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in sign
  280.         request.oauth_params.append(('oauth_signature', self.get_oauth_signature(request)))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in get_oauth_signature
  112.         uri, headers, body = self._render(request)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in _render
  186.             headers = parameters.prepare_headers(request.oauth_params, request.headers, realm=realm)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/oauthlib/oauth1/rfc5849/utils.py" in wrapper
  32.         return target(params, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/oauthlib/oauth1/rfc5849/parameters.py" in prepare_headers
  58.         escaped_value = utils.escape(value)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/oauthlib/oauth1/rfc5849/utils.py" in escape
  57.                          'Got %s of type %s.' % (u, type(u)))

Exception Type: ValueError at /social-auth/
Exception Value: Only unicode objects are escapable. Got None of type <class 'NoneType'>.

Any help would be appreciated, thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions