Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用my python函数获取完整数据时出错?_Python_Api_Python 3.6 - Fatal编程技术网

使用my python函数获取完整数据时出错?

使用my python函数获取完整数据时出错?,python,api,python-3.6,Python,Api,Python 3.6,我正在尝试从api获取产品数据 默认情况下,此api返回20个产品,如果使用api的参数Limit=500,则在单个请求中,api最多可以返回500个产品 因此,为了获取所有产品,我们需要使用另外一个参数Limit-Offset(要跳过的产品数量) 我已经编写了以下函数来实现这一点,但在完整数据的情况下,我的函数不能很好地工作,它给了我类似的错误-登录失败,签名不匹配 def get_data(userid, api_key, action, pagination=True): time

我正在尝试从api获取产品数据

默认情况下,此api返回20个产品,如果使用api的参数Limit=500,则在单个请求中,api最多可以返回500个产品

因此,为了获取所有产品,我们需要使用另外一个参数Limit-Offset(要跳过的产品数量)

我已经编写了以下函数来实现这一点,但在完整数据的情况下,我的函数不能很好地工作,它给了我类似的错误-登录失败,签名不匹配

def get_data(userid, api_key, action, pagination=True):
    timeformat = datetime.datetime.now().replace(microsecond=0).isoformat() + '+08:00'
    endpoint = 'https://example.com'
    page_json = {}
    # set required parameters for this api
    parameters = {
      'UserID': userid,
      'Version': '1.0',
      'Action': action,
      'Format': 'JSON',
      'Timestamp': timeformat
    }
    if pagination:
        page = 0
        parameters['Limit'] = 500
        while True:
            parameters['Offset'] = 500 * page
            # set the required cryptographic signature
            concatenated = urllib.parse.urlencode(sorted(parameters.items()))
            parameters['Signature'] = HMAC(api_key, concatenated.encode('utf-8'), sha256).hexdigest()
            page += 1
            try:
                response = requests.get(endpoint, params=parameters)
                page_json = response.json()
            except requests.exceptions.ConnectionError:
                print("Connection refused!")
                sleep(5)
    else:
        try:
            concatenated = urllib.parse.urlencode(sorted(parameters.items()))
            # set the required cryptographic signature
            parameters['Signature'] = HMAC(api_key, concatenated.encode('utf-8'), sha256).hexdigest()
            response = requests.get(endpoint, params=parameters)
            page_json = response.json()
        except requests.exceptions.ConnectionError:
            print("Connection refused!")
            sleep(5)

 return page_json
在完整数据的情况下,我似乎没有正确拟合签名参数行。我打印了
串联的值-

第1页 连接::Action=GetProducts&Format=JSON&Limit=500&Offset=500&Signature=3d9cd320a4bf816aeea828b9392ed2d5a27cd584b3a337338909c0ab161a101e&Timestamp=2018-05-26T12%3A58%3A38%2B08%3A00&UserID=contact%40example.com.sg&Version=1.0 尝试:{'ErrorResponse':{'Head':{'ErrorCode':'7','ErrorMessage':'E7:登录失败。签名不匹配,'ErrorType':'Sender','RequestAction':'GetProducts','RequestId':'0bb606c015273197313552686ec46f'}}

第二页 连接::Action=GetProducts&Format=JSON&Limit=500&Offset=1000&Signature=c1bda1a5ab21c4e4182cc82ca7ba87cb9fc6c5f24c36f9bb006f9da906cf7083&Timestamp=2018-05-26T12%3A58%3A38%3A00&UserID=contact%40example.com.sg&Version=1.0 尝试:{'ErrorResponse':{'Head':{'ErrorCode':'7','ErrorMessage':'E7:登录失败。签名不匹配,'ErrorType':'Sender','RequestAction':'GetProducts','RequestId':'0bb606c015273197321748243ec3a5'}

你能检查一下我的函数并帮我找出我写错了什么以及它应该是什么样子吗?

请尝试以下方法:

if pagination:
    page = 0
    parameters['Limit'] = 500
    while True:
        parameters['Offset'] = 500 * page
        # set the required cryptographic signature
        concatenated = urllib.parse.urlencode(sorted(parameters.items()))
        parameters['Signature'] = HMAC(api_key, concatenated.encode('utf-8'), sha256).hexdigest()
        page += 1
        try:
            response = requests.get(endpoint, params=parameters)
            page_json = response.json()
        except requests.exceptions.ConnectionError:
            print("Connection refused!")
            sleep(5)
        del parameters['Signature'] 

我认为您使用的dict与循环中包含签名的dict相同,因此第一个请求正常,但后续请求失败,因为签名错误。试着在每次迭代中打印连接的
。是的,它实际上正在发生-@Norrius..我已经增加了打印值。因为我们为每个请求设置不同的偏移量,所以签名也将更改。那么,您的观点是什么?请查看一下我的代码,让我知道如何正确处理它?鉴于您是基于连接的
计算签名的,我不认为上一次迭代的签名应该在该目录中。谢谢@Norrus。这很有帮助。