title: OAUTH2 - Fitbit using requests_oauthlib
body: <p><strong>Context</strong></p>
  
  <blockquote>
    <p>I am writing a python/django application which accesses Fitbit data.</p>
  </blockquote>
  
  <p><strong>Problem</strong></p>
  
  <blockquote>
    <p>To access a users data I must get their token for which can be used repeatably to access fitness data. The following goes through the current steps.</p>
  </blockquote>
  
  <hr>
  
  <p><strong>1. Firstly I present the user with a link:</strong></p>
  
  <pre><code>def register_2(request):
      if request.user.is_authenticated():
  
          oauth = OAuth2Session(
                  auth_2_client_id,
                  redirect_uri    = redirect_uri,
                  scope           = fitbit_scope)
  
          authorization_url, state = oauth.authorization_url(fitbit_url_authorise_2)
  
          return render_to_response('register.html',{
                  "authorization_url" : authorization_url
              },context_instance=RequestContext(request))
      return HttpResponseRedirect("/")
  </code></pre>
  
  <hr>
  
  <p><strong>2. User goes to Fitbit, logs into their account and authorises access.</strong></p>
  
  <hr>
  
  <p><strong>3. The user is then returned to my site with a code that should allow me to get the token.</strong> </p>
  
  <pre><code>def callback_2(request):
      if request.user.is_authenticated():
          code = request.GET.get('code')
          state = request.GET.get('state')
  
          oauth = OAuth2Session(
                  client_id               = auth_2_client_id,
                  redirect_uri            = redirect_uri
                  )
  
          token = oauth.fetch_token(
                  code                    = code,
                  token_url               = fitbit_url_access_2,
                  authorization_response  = request.build_absolute_uri()
                  )
  </code></pre>
  
  <p>Once <code>callback_2</code> is called I get the error:</p>
  
  <pre><code>(missing_token) Missing access token parameter.
  </code></pre>
  
  <p><strong>Resources</strong>:</p>
  
  <p><a href="https://wiki.fitbit.com/display/API/OAuth+2.0" rel="nofollow">Fitbit OAUTH2 API</a></p>
  
  <p><a href="https://github.com/requests/requests-oauthlib" rel="nofollow">OAuth2Session Docs</a></p>