Generate an OAuth2 token in a view
Question: Let's say I have an AngularJS application that consumes the REST API of a
Django application.

The Django application has got a built-in OAuth2 provider that can be called
to retrieve an access token and use the protected endpoints of the API. This
provider is using `django-oauth-toolkit`.

Let's assume there is a registered client with "password" grant type, so that
the end users only need to provide their credentials in the front-end in order
to get an access token from the back-end.

At some point we want to add some support for social networks login and we
decide to use `python-social-auth` (PSA) to that end. Here is the workflow I
want to achieve:

  1. The user logs in on Facebook from the front-end (via the Facebook SDK) and we get an access token back from the OAuth2 provider of Facebook.
  2. We send the Facebook token to an endpoint of our REST API. This endpoint uses the Facebook token and `django-social-auth` to authenticate the user in our Django application (basically matching a Facebook account to a standard account within the app).
  3. If the authentication succeeds, the API endpoint **requests an access token from the OAuth2 provider for this newly authenticated user**.
  4. The Django access token is sent back to the front-end and can be used to access the REST API in exactly the same way that a regular user (i.e. logged in with his credentials) would do.

Now my problem is: **how do I achieve step 3?** I first thought I would
register a separate OAuth2 client with [Client Credentials
Grant](https://tools.ietf.org/html/rfc6749#section-4.4) but then the generated
token is not user-specific so it does not make sense. Another option is to use
the `TokenAuthentication` from DRF but that would add too much complexity to
my project. I already have an OAuth server and I don't want to set up a second
token provider to circumvent my problem, unless this is the only solution.

I think my understanding of PSA and `django-oauth-toolkit` is not deep enough
to find the best way of reaching my goal, but there must be a way.
[Help!](https://www.youtube.com/watch?v=XvbWL3TlaRU)


Answer: I managed to get something working using urllib2. I can't speak towards
whether or not this is good practice, but I can successfully generate an
OAuth2 token within a view.

Normally when I'd generate an access token with cURL, it'd look like this:

    
    
    curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
    

So we're tasked with making urllib2 accomplish this. After playing around for
some bit, it is fairly straightforward.

    
    
    import urllib, urlib2, base64, json
    
    # Housekeeping
    token_url = 'http://localhost:8000/auth/token/'
    data = urllib.urlencode({'grant_type':'password', 'username':<username>, 'password':<password>})
    authentication = base64.b64encode('%s:%s' % (<client_id>, <client_secret>))
    
    # Down to Business
    request = urllib2.Request(token_url, data)
    request.add_header("Authorization", "Basic %s" % authentication)
    access_credentials = urllib2.urlopen(request)
    json_credentials = json.load(access_credentials)
    

I reiterate, I do not know if this is in bad practice and I have not looked
into whether or not this causes any issues with Django. AFAIK this will do
this trick (as it did for me).

