title: How to generate an AccessToken programmatically in Django?
body: <p>I'm setting up an API. Everything is working. I'm creating a token via OAuth2 python lib. I'm using TastyPie for my API.</p>
  
  <p>The problem I'm facing.. is that there is no "create" token method in the AccessToken or Client models.</p>
  
  <p>I can create an accessToken via the Django admin, and I can create one by doing a curl to:</p>
  
  <p>myhost.com/oauth2/access_token (with all the info, secret key, client id, user &amp; pass) </p>
  
  <p>my goal is to upon successful registration of a user with my API, the oAuth client is automatically created (working) but I also want to generate the AccessToken. I cannot cURL my own server as its giving me a redirect/connection refused error so I want to do it programmatically in Python. Anyway to do this? Here's a snippet:</p>
  
  <pre><code>try:
          user = User.objects.create_user(username, password)
          user.save()
  
          if user:
              oauth_client = Client(user=user, name="api account", client_type=1, url="http://example.com")
              oauth_client.save()
  
              oauth_client_id = oauth_client.pk
              oauth_client_secret = oauth_client.client_secret
  
          if oauth_client:
              print user
              print oauth_client_id
              print AccessToken.objects.all()
              print '........'
              token = AccessToken(user=user, client=oauth_client_id, scope=6)
              token.save()
  </code></pre>
  
  <p>the last two lines above, while giving NO errors.. will NOT save a new AccessToken.</p>