Search code examples
myob

Myob Accountright Live


I am trying to create a new customer in sandbox account of accountright Live api v2. I am able to obtain access token using the sample php oAuth class. Now when i send a request for creating new customer, i get a 403 forbidden response. The code i am using for create customer is:

    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);
}

Please suggest if i have missed something.


Solution

  • In my code the $url was wrong, using the correct url i was able to make it execute. The correct url:

    $url = 'https://api.myob.com/accountright/c3ee2b2a-6b8f-4d36-bef5-5e0c89bf104a/Contact/Customer';