Failed Authentication Error while calling Australia Post Shipments API

Question:

While following the example of create-shipments ,i am getting this error response

{
    "errors": [
        {
            "message": "The request failed authentication",
            "error_code": "API_001",
            "error_name": "Unauthenticated request"
        }
    ]
}

here is my example code:

import json,requests
headers = {"account-number":"1234567890",'auth-key':'aaaaaaaa-1111-2222-927d-c793f4fb1461','Content-Type': 'application/json'}
print requests.post('https://digitalapi.auspost.com.au/shipping/v1/shipments', data=json.dumps(data), headers=headers).content

and data is something like:

data={
"shipments":[
    {
        "shipment_reference":"XYZ-s001-01",
        "customer_reference_1":"Order S00o1",
        "customer_reference_2":"SKU-1",
        "email_tracking_enabled":True,
        "from":{
            "name":"prakash sharma",
            "lines": [
                "1 Main Street"
            ],
            "suburb": "MELBOURNE",
            "state": "VIC",
            "postcode": "3000",
            "phone": "0401234567",
            "email":"[email protected]"
        },
        "to":{
            "name":"Jane Smith",
            "business_name":"Smith Pty Ltd",
            "lines":[
                "123 Centre Road"
            ],
            "suburb":"Sydney",
            "state":"NSW",
            "postcode":"2000",
            "phone":"0412345678",
            "email":"[email protected]"                
        },
        "items":[
            {
                "item_reference":"SKU-1",
                "product_id":"T28S",
                "length":"10",
                "height":"10",
                "width":"10",
                "weight":"1",
                "authority_to_leave":False,
                "allow_partial_delivery":True,
                "features":{  
                    "TRANSIT_COVER":{  
                         "attributes":{  
                             "cover_amount":1000
                          }
                     }
                 }
            }

        ]
    }
]

}

My auth key and account-number are valid as it work in case of GET request over /postage/parcel/international/service.json and /postage/parcel/domestic/service.json

Asked By: Prakash Kumar

||

Answers:

Problem is solved, as shipping/v1/shipments required Basic Authorization so we need to set the header as
:

headers={'Content-Type':'application/json',
        'Authorization':'Basic '+base64.b64encode(API_Key+":"+Password_Secret),
        'Account-Number':AccountNumber}

OR

we can use HTTPBasicAuth as auth=HTTPBasicAuth(API_Key, Password_Secret) in the requests.post.

Answered By: Prakash Kumar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.