Created
August 30, 2019 18:58
-
-
Save reece/36ecf539210d2448e5b29d931fe4054a to your computer and use it in GitHub Desktop.
FreshBooks OAuth2 authentication example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FreshBooks OAuth2 authentication example | |
# Based on examples from | |
# https://requests-oauthlib.readthedocs.io/en/latest/examples/examples.html | |
# Credentials you get from registering a new application | |
# at https://my.freshbooks.com/#/developer | |
client_id = '<the id you get from FreshBooks>' | |
client_secret = '<the secret you get from FreshBooks>' | |
# OAuth endpoints from FreshBooks API documentation | |
# https://www.freshbooks.com/api/start | |
# and https://www.freshbooks.com/api/postman-collection (terrific!) | |
authorization_base_url = "https://my.freshbooks.com/service/auth/oauth/authorize" | |
token_url = "https://api.freshbooks.com/auth/oauth/token" | |
# Redirect URI *must* be one the redirect uris in the app configuration | |
redirect_uri = "https://localhost:6666/" | |
from requests_oauthlib import OAuth2Session | |
sess = OAuth2Session(client_id, redirect_uri = redirect_uri) | |
# Redirect user to FreshBooks for authorization | |
authorization_url, state = sess.authorization_url(authorization_base_url) | |
print('Please go here and authorize,', authorization_url) | |
# Get the authorization verifier code from the callback url | |
redirect_response = input('Paste the full redirect URL here:') | |
# Fetch the access token | |
sess.fetch_token(token_url, client_secret = client_secret, | |
authorization_response = redirect_response) | |
# Fetch a protected resource, i.e. user profile | |
r = sess.get('https://api.freshbooks.com/auth/api/v1/users/me') | |
print(r.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment