-
-
Save gaslitbytech/00ad6805a948666d399b to your computer and use it in GitHub Desktop.
Attempted to modify to work with http://developer.servicem8.com/docs/platform-services/authentication/
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
from requests_oauthlib import OAuth2Session | |
from flask import Flask, request, redirect, session, url_for | |
from flask.json import jsonify | |
import os | |
from uuid import uuid4 | |
#from urlparse import urlparse, urljoin | |
app = Flask(__name__) | |
# This information is obtained upon registration of a new GitHub OAuth | |
# application here: https://github.com/settings/applications/new | |
client_id = "ID" | |
client_secret = "SECRET" | |
authorization_base_url = 'https://www.servicem8.com/oauth/authorize' | |
token_url = 'https://www.servicem8.com/oauth/access_token' | |
grant_type = 'authorization_code' | |
scope = ['manage_inventory', 'manage_job_materials'] | |
duration = 'permanent' | |
response_type = 'code' | |
@app.route("/") | |
def demo(): | |
redirect_uri = url_for('.callback', _external = True) | |
state = str(uuid4()) | |
"""Step 1: User Authorization. | |
Redirect the user/resource owner to the OAuth provider (i.e. ServiceM8) | |
using an URL with a few key OAuth parameters. | |
""" | |
servicem8_client = OAuth2Session(client_id, redirect_uri = redirect_uri, scope = scope) | |
authorization_url, state = servicem8_client.authorization_url(authorization_base_url, Response_type = response_type) | |
# State is used to prevent CSRF, keep this for later. | |
session['oauth_state'] = state | |
return redirect(authorization_url) | |
# Step 2: User authorization, this happens on the provider. | |
@app.route("/callback", methods=["GET"]) | |
def callback(): | |
code = request.args.get('code') | |
""" Step 3: Retrieving an access token. | |
The user has been redirected back from the provider to your registered | |
callback URL. With this redirection comes an authorization code included | |
in the redirect URL. We will use that to obtain an access token. | |
if (oauth.refresh_token != null) | |
{ | |
body.Add(new KeyValuePair<string, string>("grant_type", "refresh_token")); | |
body.Add(new KeyValuePair<string, string>("refresh_token", oauth.refresh_token)); | |
} | |
else | |
{ | |
body.Add(new KeyValuePair<string, string>("grant_type", "authorization_code")); | |
body.Add(new KeyValuePair<string, string>("code", code)); | |
} | |
body.Add(new KeyValuePair<string, string>("redirect_uri", redirect_uri)); | |
body.Add(new KeyValuePair<string, string>("state", state)); | |
body.Add(new KeyValuePair<string, string>("client_id", client_id)); | |
body.Add(new KeyValuePair<string, string>("client_secret", client_secret)); | |
""" | |
servicem8_client = OAuth2Session(client_id, state=session['oauth_state']) | |
token = servicem8_client.fetch_token(token_url, client_secret=client_secret, code = code, | |
authorization_response=request.url) | |
# At this point you can fetch protected resources but lets save | |
# the token and show how this is done from a persisted token | |
# in /profile. | |
session['oauth_token'] = token | |
return redirect(url_for('.profile')) | |
@app.route("/profile", methods=["GET"]) | |
def profile(): | |
"""Fetching a protected resource using an OAuth 2 token. | |
""" | |
servicem8_client = OAuth2Session(client_id, token=session['oauth_token']) | |
return jsonify(servicem8_client.get('https://api.servicem8.com/api_1.0/JobMaterial.json').json()) | |
if __name__ == "__main__": | |
# This allows us to use a plain HTTP callback | |
os.environ['DEBUG'] = "1" | |
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' | |
app.secret_key = os.urandom(24) | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Current Issue is
TypeError: prepare_request_uri() got multiple values for keyword argument 'scope'