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

时间:2018-05-26 07:36:41

标签: python api python-3.6

我正在尝试从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

在完整数据的情况下,我似乎无法正确拟合我的签名参数行。我打印了concatenated的值,它看起来像 -

页面是1 concatenated :: 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:登录失败。签名不匹配','错误类型'发送者',' RequestAction':' GetProducts',' RequestId' :' 0bb606c015273197313552686ec46f'}}}

页面是2 concatenated :: Action = GetProducts& Format = JSON& Limit = 500& Offset = 1000& Signature = c1bda1a5ab21c4e4182cc82ca7ba87cb9fc6c5f24c36f9bb006f9da906cf7083& Timestamp = 2018-05-26T12%3A58%3A38%2B08%3A00& UserID = contact%40example.com.sg& Version = 1.0 尝试:{' ErrorResponse':{' Head':{' ErrorCode':' 7',' ErrorMessage': ' E7:登录失败。签名不匹配','错误类型'发送者',' RequestAction':' GetProducts',' RequestId' :' 0bb606c015273197321748243ec3a5'}}}

你能看看我的功能并帮助我找出我写错了什么以及它应该是什么样的?

1 个答案:

答案 0 :(得分:0)

请试试这个:

char* p = malloc(10);
p = p + 1;
free(p);