getting error while fetching full data with my python function? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of getting error while fetching full data with my python function? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to fetch product data from an api.

By default this api returns 20 products and in a single request the api can return max 500 products if we use api’s parameter Limit=500.

So for fetching all products we need to use one more parameter with Limit- Offset(Number of products to skip).

I have written following function to achieve this but in case of full data my function is not working well and it’s giving me error like- Login failed, Signature mismatching.

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

It looks like I am not fitting my signature parameter line correctly in case of full data.I printed the value of concatenated and it looks like-

page is 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 try: {‘ErrorResponse’: {‘Head’: {‘ErrorCode’: ‘7’, ‘ErrorMessage’: ‘E7:Login failed. Signature mismatching’, ‘ErrorType’: ‘Sender’, ‘RequestAction’: ‘GetProducts’, ‘RequestId’: ‘0bb606c015273197313552686ec46f’}}}

page is 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 try: {‘ErrorResponse’: {‘Head’: {‘ErrorCode’: ‘7’, ‘ErrorMessage’: ‘E7:Login failed. Signature mismatching’, ‘ErrorType’: ‘Sender’, ‘RequestAction’: ‘GetProducts’, ‘RequestId’: ‘0bb606c015273197321748243ec3a5’}}}

Can you please look into my function and help me to find out what I have written wrong and what it should be like?

Answer

Try this please:

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'] 
We are here to answer your question about getting error while fetching full data with my python function? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji