2016-06-13 68 views
0

雖然下面的create-shipments的例子中,我得到這個錯誤響應失敗的驗證錯誤,同時呼籲澳大利亞郵政發貨API

{ 
    "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": "04", 
      "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 
          } 
        } 
       } 
      } 

     ] 
    } 
] 

}

auth keyaccount-number是有效的,因爲它在GET請求的情況下,工作在/postage/parcel/international/service.json/postage/parcel/domestic/service.json

回答

0

問題被解決了,shipping/v1/shipments所需的基本授權,所以我們需要設置標題爲 :

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)

相關問題