Python 使用oauthlib请求修改授权URL格式

Python 使用oauthlib请求修改授权URL格式,python,oauth,python-requests,etrade-api,Python,Oauth,Python Requests,Etrade Api,我正在使用请求oauthlib通过ETrade API进行身份验证。它要求授权URL具有以下格式: https://us.etrade.com/e/t/etws/authorize?key={oauth_consumer_key}&token={oauth_token} 但是,当我调用authorization\u url()时,它对该参数使用oauth\u令牌,而不是token。目前我自己使用format()格式化URL,但现在我有token和oauth\u token参数。这是可行

我正在使用
请求oauthlib
通过ETrade API进行身份验证。它要求授权URL具有以下格式:

https://us.etrade.com/e/t/etws/authorize?key={oauth_consumer_key}&token={oauth_token}
但是,当我调用
authorization\u url()
时,它对该参数使用
oauth\u令牌
,而不是
token
。目前我自己使用
format()
格式化URL,但现在我有
token
oauth\u token
参数。这是可行的,但完全不雅观。是否有方法修改
授权\u url()
的行为,以允许使用我需要的url格式

为了完整起见,以下是我的代码:

oauth_session = requests_oauthlib.OAuth1Session(config.oauth_consumer_key, config.consumer_secret, callback_uri='oob')


def get_request_token():
    path = 'oauth/request_token'
    url = base_url + path
    return oauth_session.fetch_request_token(url)


def get_authorization_url(request_token):
    url_format = 'https://us.etrade.com/e/t/etws/authorize?key={oauth_consumer_key}&token={oauth_token}'
    url = url_format.format(oauth_consumer_key=config.oauth_consumer_key, oauth_token=request_token['oauth_token'])
    return oauth_session.authorization_url(url)

request_token = get_request_token()
print(get_authorization_url(request_token))

authorization\u url()
函数是一个方便的函数,它调用一个通用函数来向url添加查询参数(OAuthLib的
common.add\u params\u to\u uri()
,后者反过来使用
urlparse.urlunprase()
)。无法获取
authorization\u url()
以省略
oauth\u令牌
参数

返回的类型是
str
(在Python 3中),因此只要您确定url和标记将只包含有效字符,就可以通过使用普通字符串格式调用获得与函数中相同的结果

但是,如果附加的
oauth\u令牌
参数没有引起任何问题,我建议使用
authorization\u url()
函数来提供额外的安全性

此外,无需在函数中执行额外的
str.format()
调用,
authorization\u url()
采用
kwargs
,可用于指定这些参数:

def get_authorization_url(request_token):
    url = 'https://us.etrade.com/e/t/etws/authorize'
    return oauth_session.authorization_url(url,
                                           key=config.oauth_consumer_key, 
                                           token=request_token['oauth_token'])

据我所知,额外的
oauth\u令牌
参数不会导致任何问题。
authorization\u url()
提供了什么额外的安全性?它对令牌进行URL编码吗?还有别的吗?