我正在使用ringCentral的API,并尝试使用python 3的密码机制来获取身份验证令牌。之前,我是使用python 2进行的,但仍可以正常工作。根据API文档中的要求,我使用urllib.urlencode()方法对请求参数进行URL编码。等效于python 3中的该方法的是urllib.parse.urlencode(),但是在python 3中执行时,我遇到了以下错误。
{"error": "invalid_request", "errors": [{"errorCode": "OAU-156", "message":
"Basic authentication header is missing or malformed"}], "error_description":
"Basic authentication header is missing or malformed"}
在两种情况下,我甚至都从urlencode方法打印出结果字符串,并且是相同的。我不明白这是什么问题?有什么见解吗?我也找不到带有该错误代码的任何信息。
Python 3代码:
import urllib.parse
import json
import requests
basic="%s:%s" % ("<my cllient id>","<my cllient secret>")
auth_header = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"Authorization": "Basic "+ str(base64.b64encode(basic.encode()))
}
body = urllib.parse.urlencode({
'grant_type': 'password',
'username': "<my number>",
'password': "<my password>"
})
auth_request=requests.request("POST","https://platform.devtest.ringcentral.com/restapi/oauth/token",headers=auth_header,data=body)
print(json.dumps(auth_request.json()))
Python 2代码(有效):
import urllib
import json
import requests
basic="%s:%s" % ("<my cllient id>","<my cllient secret>")
auth_header = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"Authorization": "Basic "+ str(base64.b64encode(basic.encode()))
}
body = urllib.urlencode({
'grant_type': 'password',
'username': "<my number>",
'password': "<my password>"
})
auth_request=requests.request("POST","https://platform.devtest.ringcentral.com/restapi/oauth/token",headers=auth_header,data=body)
print(json.dumps(auth_request.json()))
您需要将您的代码放在这里,以便我们对问题进行故障排除。同时,您能否使用Python 2和3检查RingCentral,您可以在此处逐步学习如何使用Python本机API访问RingCentral服务:https://ringcentral-tutorials.github.io/call-ringcentral-apis-native-python-demo/?distinctId=171e1c5b614e8-078e90c97b438a-1d346655-fa000-171e1c5b615fa#0
您可以找到它如何访问状态码,定义和处理令牌以及处理授权