2016-06-13 84 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)