调用Australia Post Shipments API时失败的身份验证错误

时间:2016-06-13 14:12:35

标签: python json rest python-requests shipping

在关注create-shipments的示例时,我收到此错误回复

WindowStartupLocation="CenterScreen"

这是我的示例代码:

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

和数据类似:

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

}

我的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":"prakashsharmacs24@gmail.com" }, "to":{ "name":"Jane Smith", "business_name":"Smith Pty Ltd", "lines":[ "123 Centre Road" ], "suburb":"Sydney", "state":"NSW", "postcode":"2000", "phone":"0412345678", "email":"jane.smith@smith.com" }, "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 } } } } ] } ] auth key有效,因为GET请求优先于account-number/postage/parcel/international/service.json

1 个答案:

答案 0 :(得分:0)

问题已解决,因为shipping/v1/shipments需要基本授权,因此我们需要将标头设置为 :

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

OR

我们可以在requests.post中使用HTTPBasicAuth作为auth=HTTPBasicAuth(API_Key, Password_Secret)