安装程序:Python 2.7.10,请求库,Windows 8.1
开发人员还给了我一个承载令牌来传递他们声称应该在所有测试中都有效的头,当我在Postman中尝试它时,它似乎确实有效(虽然我确实有一些混合结果),但由于某种原因,当我尝试在Python请求设置中使用它时,每次都会返回一个错误。代码如下(必须将工作数据更改为虚拟数据,因此尝试按原样运行以下代码将不起作用,但希望看到设置可以帮助确定我可能做错了什么):
import requests
from pprint import pprint
base_url = "https://rpc_url.com/rpc"
custom_headers = {"Content-Type":"application/json", "Authorization":"Bearer token"}
'''Sign in payload'''
signinPayload = {"method" : "identity.authenticate",
"id" : 1,
"jsonrpc" : "2.0",
"params" : {"password" : "password", "username" : "username"}}
test1_Payload = {"jsonrpc" : "2.0",
"method" : "fc.pick.getPendingPicks",
"id" : 20, "params" :{"_tags" :{"device" : "device",
"deviceOS" : "deviceOS", "firstName" : "firstName",
"devicetype" : "devicetype", "appVersion" : "appVersion",
"login" : "username", "devicelabel" : "devicelabel",
"lastName" : "lastName"}}}
with requests.Session() as s:
# below passes the login payload, which returns proper data.
login_post = s.post(base_url, json=signinPayload, headers=custom_headers).json()
pprint(login_post)
###########################
# passes the test payload, which doesn't work correctly
r = s.post(base_url, json=test1_Payload, headers=custom_headers).json()
pprint(r)
我已经尝试了许多变体,例如不在头中传递承载令牌,只传递承载令牌而不传递登录有效载荷,到目前为止没有任何效果。
当我使用非虚拟数据运行上述代码时,这是控制台输出(为了清楚起见,我添加了注释):
# Below is returned for sign in authenticate, which appears to be good data
{u'fcFlingVersion': u'1.0.9',
u'id': 1, u'jsonrpc':
u'2.0',
u'result': {
u'_idp': {u'accessToken': 'accessTokenNumber'},
u'administratorId': 1,
u'changePassword': False,
u'companyId': 1,
u'daysUntilPasswordExpires': 100,
u'firstname': u'firstName',
u'groupList': u'groupList',
u'lastname': u'lastName',
u'login': u'username',
u'passwordDirectory': u'identity',
u'photoUrl': u'https://photoURL',
u'roles': [],
u'userId': 1,
u'username': u'username'}}
# Below is the returned error data for test1_Payload after the successful sign in test from above.
{u'error': {u'code': 404, u'data': None, u'message': u'module not found'},
u'fcFlingVersion': u'1.0.9',
u'id': 20,
u'jsonrpc': u'2.0'}
问题是“未找到模块”响应出错,它应该返回该请求的相关数据。