呼叫澳大利亚时身份验证失败 Post 发货 API

Failed Authentication Error while calling Australia Post Shipments API

在遵循 create-shipments 的示例时,我收到此错误响应

{
    "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 keyaccount-number 是有效的,因为它在 /postage/parcel/international/service.json/postage/parcel/domestic/service.json

上的 GET 请求的情况下有效

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

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

我们可以在 requests.post.

中将 HTTPBasicAuth 用作 auth=HTTPBasicAuth(API_Key, Password_Secret)