【问题标题】:oauth2 requests-oauthlib TypeError: prepare_request_uri() got multiple values for keyword argument 'scope'oauth2 requests-oauthlib TypeError:prepare_request_uri() 为关键字参数“范围”获取了多个值
【发布时间】:2015-01-31 10:52:11
【问题描述】:

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

【问题讨论】:

    标签: python flask oauth-2.0


    【解决方案1】:

    通过将这些参数移动到构造函数而不是 authentication_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)
    

    【讨论】:

    • 您是如何克服回调页面上的安全连接失败错误的?
    猜你喜欢
    • 2013-08-04
    • 2016-06-24
    • 2015-05-18
    • 1970-01-01
    • 2013-03-09
    • 2016-04-25
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多