代码之家  ›  专栏  ›  技术社区  ›  chmcc

JSON-RPC-无法使用Python请求库从登录站点检索正确的POST数据响应

  •  0
  • chmcc  · 技术社区  · 7 年前

    安装程序: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'}
    

    问题是“未找到模块”响应出错,它应该返回该请求的相关数据。

    1 回复  |  直到 7 年前
        1
  •  0
  •   chmcc    7 年前

    我最终发现问题是由于API中最近的URL更改导致的,我没有得到通知,这解释了为什么URL最初在Postman中工作。

    更让人困惑的是,只更改了一些方法的URL,这就是为什么要更改登录标识。验证帖子有效,因为它仍然使用原始URL。对于其他POST命令,方法/模块不再与原始URL一起出现,因此出现错误消息“未找到模块”

    当我用接收错误消息的有效负载的新base\u url更新base\u url变量后,它们返回了正确的数据。

    总而言之:代码工作正常,输入到代码中的数据有缺陷,以及接收错误消息的原因。在本例中,有缺陷的数据是base_url变量指向了过时的url。