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

Close the window\tab once CherryPy server accepted authentication response #135

Open
brandonmichaelhunter opened this issue Jun 14, 2018 · 2 comments

Comments

@brandonmichaelhunter
Copy link

Is there a way to automatically close the new window or tab that opens the authorization from fitbit
threading.Timer(1, webbrowser.open, args=(url,)).start()

I would like to close the browser after cherrypy.quickstart(self) to close the window or tab

@brad
Copy link
Member

brad commented Mar 5, 2019

@brandonmichaelhunter I don't think it is possible with webbrowser.open but it may be possible using something like cefpython this is a great task for a community member to contribute. A PR would be most welcome

@MasonV
Copy link

MasonV commented Aug 31, 2019

This really should be an option, although it would mean switching from webbrowser to a more flexible module like Selenium if I'm not mistaken.

It is a bit hard to read, but if you dive into api.py there's actually a bit of a workaround through trying to use old access & refresh tokens. For example, I download my sleep log daily and use the following code to try the old tokens and refresh them if they don't' work.

	fb_keys = load_json("keys.json")
	auth2_client = fitbit.Fitbit(
				fb_keys['CLIENT_ID'],
				fb_keys['CLIENT_SECRET'],
				access_token = fb_keys['access_token'],
				refresh_token = fb_keys['refresh_token'],
				expires_at = math.floor(fb_keys['expires_at']),
				redirect_uri = 'http://127.0.0.1:8080/' 
				)
	try:	#Try to use old tokens
		data = auth2_client.get_sleep(date=last_datetime)['summary']
	except Exception as e:
		auth2_client = OAuth2Server(fb_keys['CLIENT_ID'],fb_keys['CLIENT_SECRET'])
		auth2_client.browser_authorize()

		tokens = auth2_client.fitbit.client.session.token
		fb_keys['access_token'] = tokens['access_token']
		fb_keys['refresh_token'] = tokens['refresh_token']
		fb_keys['expires_at'] = tokens['expires_at']
		
		dump_json("keys.json",fb_keys)	 	

Note: the expires_at that is returned is a Unix Timestamp, but the fitbit wrapper only accepts integer values.

Also the default expiry time of the token is pretty short so it isn't all too useful anyways. I modified the API to pass the expires_in parameter to the authorization page, but didn't see any change in the final expiry date :(

Edit for like the billionth time: Looks like this wrapper uses the Authorization code grant flow, not the implicit flow, so the expires_in paramter can't be used. Guess I'll have to figure out how to use the refresher.

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

No branches or pull requests

3 participants