2

我正在尝试在 accountright Live api v2 的沙盒帐户中创建一个新客户。我可以使用示例 php oAuth 类获取访问令牌。现在,当我发送创建新客户的请求时,我收到 403 禁止响应。我用于创建客户的代码是:

    function saveContact($type, $contactId, $CoLastName, $FirstName, $IsActive, $TaxCodeId, $FreightTaxCodeId) {
    global $apiBaseURL;


    $url =  'https://ar1.api.myob.com/accountright/c3ee2b2a-6b8f-4d36-bef5-5e0c89bf104a/Contact/Customer';
    $param = '{
    "LastName": "Kumar",
    "FirstName": "Amit",
    "IsIndividual": true,
    "IsActive": true,
    "SellingDetails": {
        "TaxCode": {
            "UID": "352a8200-bf57-4723-9165-9f80429afd7d"
        },
        "FreightTaxCode": {
            "UID": "352a8200-bf57-4723-9165-9f80429afd7d"
        }
    }
}';

    // build the cftoken
  $cftoken = base64_encode('Administrator:');

      $headers = array(
        'Authorization: Bearer '.$_SESSION['access_token'],
        'x-myobapi-cftoken: '.$cftoken,
        'x-myobapi-key: '.api_key,
        'x-myobapi-version: v2',
        'Content-Type: application/json',                                                                                

    );


    $session = curl_init($url); 



//  curl_setopt ($session, CURLOPT_HTTPHEADER, $headers);

    // Tell curl to use HTTP POST
    curl_setopt ($session, CURLOPT_POST, true); 
    // Tell curl that this is the body of the POST
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params); 
    // setup the authentication
    curl_setopt($session, CURLOPT_USERPWD, "Administrator:");
    curl_setopt($session, CURLOPT_HEADER, $headers); 
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt(CURLOPT_SSL_VERIFYPEER, true); // enforce that when we use SSL the verification is correct



    $response = curl_exec($session); 
    var_dump($response);

    curl_close($session);

    return($response);
}

如果我错过了什么,请提出建议。

4

2 回答 2

0

嗯,从你的代码中,我想你可能需要这个:'x-myobapi-cftoken: [Base64Encode(username:password)]'。只是为了确保您拥有正确的登录详细信息。

此外,我认为您在请求内容中还需要 CompanyName 。

于 2016-11-04T07:42:25.050 回答
0

在我的代码中 $url 是错误的,使用正确的 url 我能够使其执行。正确的网址:

$url = 'https://api.myob.com/accountright/c3ee2b2a-6b8f-4d36-bef5-5e0c89bf104a/Contact/Customer';
于 2018-12-07T08:51:55.040 回答