Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback URL not being invoked #32

Closed
karthikarul20 opened this issue May 16, 2014 · 2 comments
Closed

Callback URL not being invoked #32

karthikarul20 opened this issue May 16, 2014 · 2 comments

Comments

@karthikarul20
Copy link

I am trying to integrate the FITBIT api in which i managed to get auth_token and redirect the user for Authorization. While creating the Fitbit() i am setting the callback_uri. But, after completing the authorization the FitBit API is not invoking my callback service.

Please pardon my ignorance and help me to deal with this issue.

I am using Web2Py framework and below is my code.

def index():
    callback_uri=getHost()+URL('callback','completeAuthorization')
    from fitbit import Fitbit
    from requests_oauthlib import OAuth1Session
    client_kwargs = {
            'client_key': 'c8ca82f890c44a969cc3524b7c342e8b',
            'client_secret': '7bf3dd8b933144e2bbe2c7971d46e3ae',
            'user_key': None,
            'user_secret': None,
            'callback_uri': callback_uri
        }
    fb = Fitbit(**client_kwargs)
    retval = fb.client.fetch_request_token()
    url='https://api.fitbit.com/oauth/authorize?oauth_token='+retval.get('oauth_token')+'&callback_uri='+callback_uri
    print callback_uri
    redirect(url)
    return dict(message=url)

def completeAuthorization():
    print request
    print request.vars
    print request.args
    oauth_token=request.vars.oauth_token
    oauth_verifier=request.vars.oauth_verifier
    return locals

def getHost():
    request_host_uri = URL(args=request.args, vars=request.vars, host=True)
    request_uri = URL(args=request.args, vars=request.vars, host=False)
    parts = request_host_uri.split(request_uri)
    host = parts[0]
    return str(host)

EDIT(brad): I wrapped your code to make it easier to read

@brad
Copy link
Member

brad commented May 17, 2014

Hi @karthikarul20
I think I am to blame for your troubles. If there was better documentation it probably would be easier for you.
So a couple things:

  • The authorize url you are using is wrong, the base should be www.fitbit.com instead of api.fitbit.com.
  • There is no need to build the authorize url yourself, we have a function for that; authorize_token_url. I've included some example code below.
  • The most recent version of the library changed the user_key and user_secret kwargs to resource_owner_key and resource_owner_secret respectively, but None is the default so they don't need to be specified here.
  • Fitbit will only redirect to your callback_uri if the app type on dev.fitbit.com is "Browser"
  • Now that you have publicly posted your Fitbit app key/secret, I highly recommend you reset them.
import webbrowser
from fitbit import Fitbit

client_kwargs = {
    'client_key': 'c8ca82f890c44a969cc3524b7c342e8b',
    'client_secret': '7bf3dd8b933144e2bbe2c7971d46e3ae',
    'callback_uri': 'http://127.0.0.1/test'
}
fb = Fitbit(**client_kwargs)
fb.client.fetch_request_token()
webbrowser.open(fb.client.authorize_token_url())

Let me know how that works out for you.

@karthikarul20
Copy link
Author

Thanks Brad. App type on dev.fitbit.com was the culprit. It was set as Desktop and now it works fine.
Also i have reset the client_key and client_secret as you recommended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants