0

With django-social-auth, it allows you to capture certain details and as a result a Dropbox users extra_data looks as follows

{"access_token": "oauth_token_secret=XXXXXXXXXXXX&oauth_token=YYYYYYYYYYY", "7200": null, "id": null}

The models.py is set up as follows.

class UserSocialAuth(models.Model):
    """Social Auth association model"""
    user = models.ForeignKey(User, related_name='social_auth')
    provider = models.CharField(max_length=32)
    uid = models.CharField(max_length=255)
    extra_data = JSONField(blank=True)

How do I access oauth_token and oauth_token_secret separately?

ApPeL
  • 4,801
  • 9
  • 47
  • 84

2 Answers2

1

Parse the JSON data with simplejson module:

from django.utils.simplejson import loads

data = {"access_token": "oauth_token_secret=XXXXXXXXXXXX&oauth_token=YYYYYYYYYYY", "7200": null, "id": null} tokens = parse_qs(loads(data)['access_token'])

json_dict = loads(data)
access_token = json_dict['access_token']

Then use what Jan suggested to you to parse the query string stored in access_token:

from urlparse import parse_qs

tokens = parse_qs(access_token)

print tokens['oauth_token_secret']
print tokens['oauth_token']
caio
  • 1,949
  • 16
  • 20
0

For parsing the access_token, you can use http://docs.python.org/library/urlparse.html#urlparse.parse_qs:

Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130