servicem8 api(http://developer.servicem8.com/docs/platform-services/authentication/)具有参数范围作为oauth身份验证的一部分。虽然当我包含这个值时,我似乎得到了这个错误。
TypeError:prepare_request_uri()为关键字参数'范围'
获取了多个值# 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)
authorization_url, state = servicem8_client.authorization_url(authorization_base_url, redirect_uri = redirect_uri, scope = scope)
# State is used to prevent CSRF, keep this for later.
session['oauth_state'] = state
return redirect(authorization_url)
这是使用requests-oauthlib时包含额外参数的方式吗?有没有办法避免这种冲突?
完整的要点是:https://gist.github.com/darwindave/00ad6805a948666d399b
答案 0 :(得分:1)
通过将这些参数移动到构造函数而不是authorization_url方法来修复它。
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)
完整示例
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)