OAuthException:无法使用Salesforce生成请求令牌

时间:2016-06-29 14:06:11

标签: python oauth salesforce

我正面临一个似乎无法解决的问题,尽管我已尽力而为。

我正在使用Flask + Flask_oauthlib连接到SalesForce。这是我的代码:

from flask import Flask, url_for
from flask_oauthlib.client import OAuth

app = Flask(__name__)
oauth = OAuth(app)


salesforce = oauth.remote_app('salesforce',
    consumer_key='my_consumer_key',
    # grant_type='authorization_code',
    consumer_secret='my_consumer_secret',
    request_token_url='https://login.salesforce.com/services/oauth2/token',
    access_token_url='https://login.salesforce.com/services/oauth2/token',
    authorize_url='https://login.salesforce.com/services/oauth2/authorize')

@app.route('/')
def home():
    url = url_for('oauth', _external=True)
    salesforce.authorize(callback=url)
    return 'ok'


@app.route('/oauth')
def oauth():
    resp = salesforce.authorized_response()
    print resp

    return 'ok'


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

当我运行这个并转到/时,我得到以下异常:

  

OAuthException:无法生成请求令牌

我在相关行中添加了一个输出异常行的输出(“/usr/lib64/python2.7/site-packages/flask_oauthlib/client.py”,第580行),这是Salesforce的响应:

{u'error_description': u'grant type not supported', u'error': u'unsupported_grant_type'}

所以显然我需要将grant_type设置为允许的内容。

我尝试添加grant_type,正如您在注释部分中看到的那样,但是当我重新启动代码时,它会因以下错误而停止:

  

TypeError: init ()得到了一个意外的关键字参数'grant_type'

是的......

我看了看,并运用了以下问题的答案,没有任何运气:

有没有人知道我为什么会遇到这个问题?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案,这真的很恶毒!

salesforce = oauth.remote_app('salesforce',
    consumer_key='my_consumer_key',
    consumer_secret='my_consumer_secret',
    access_token_url='https://login.salesforce.com/services/oauth2/token',
    authorize_url='https://login.salesforce.com/services/oauth2/authorize')

看不出差异?我不得不删除request_token_url,这就是全部!

现在有效!