我正在尝试按照这些说明中的 OAuth 流程向 Quizlet 发出发布请求https://quizlet.com/api/2.0/docs/authorization-code-flow
。我遇到了一个问题,在第 2 步中,我必须使用从他们的服务器生成的 token 发出发布请求,但我没有成功地将 token 传递到 url。我知道它是正确生成的,但我在传递它时遇到了问题,并且没有得到 400
响应。
更直接地说,我的问题是,是否有另一种方法可以包含我尝试通过帖子请求中的网址传递的 grant_type
和 code
参数比如通过post请求的header传递它们?我查看了请求
的文档,但没有运气。
@app.route('/')
@app.route('/index')
def index():
code = request.args.get('code')
state = request.args.get('state')
print("code is " + code)
r = requests.post("https://api.quizlet.com/oauth/token?grant_type=authorization_code&code=" + code)
return render_template('index.html')
最佳答案
您必须指定所需的 header Authorization
、Content-Type
。
import requests
from requests.auth import _basic_auth_str
client_id = 'YOUR CLIENT ID'
secret = 'YOUR CLIENT SECRET'
code = 'CODE FROM STEP 1'
headers = {
'Authorization': _basic_auth_str(client_id, secret),
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('https://api.quizlet.com/oauth/token?grant_type=authorization_code&code={0}'.format(
code), headers=headers)
print r.status_code
print r.content
关于Python请求参数未通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37062673/