Skip to content
Advertisement

NetSuite connect using PHP oAuth 1.0 getting error

Hi I want to connect netsuite using PHP . I tried in Postman and it is working properly . Request is sending and i am getting result .

So i want to execue the same thing using PHP . So i wirte the following code .

<?php

define("NETSUITE_URL", 'https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl');
define("NETSUITE_SCRIPT_ID", 'xx');
define("NETSUITE_DEPLOY_ID", 'xx');
define("NETSUITE_ACCOUNT", 'xxxx');
define("NETSUITE_CONSUMER_KEY", 'xxxxxxx');
define("NETSUITE_CONSUMER_SECRET", 'xxxxxx');
define("NETSUITE_TOKEN_ID", 'xxxxxx');
define("NETSUITE_TOKEN_SECRET", 'xxxxxx');

$details = array(
    'status' => 'Testing'
);

sendOrderToNS($details);
function sendOrderToNS($details)
{
    $data_string = json_encode($details);

    $oauth_nonce = md5(mt_rand());
    $oauth_timestamp = time();
    $oauth_signature_method = 'HMAC-SHA1';
    $oauth_version = "1.0";

    $base_string = "POST&" . urlencode(NETSUITE_URL) . "&" . urlencode("deploy=" . NETSUITE_DEPLOY_ID . "&oauth_consumer_key=" . NETSUITE_CONSUMER_KEY . "&oauth_nonce=" . $oauth_nonce . "&oauth_signature_method=" . $oauth_signature_method . "&oauth_timestamp=" . $oauth_timestamp . "&oauth_token=" . NETSUITE_TOKEN_ID . "&oauth_version=" . $oauth_version . "&realm=" . NETSUITE_ACCOUNT . "&script=" . NETSUITE_SCRIPT_ID);
    $sig_string = urlencode(NETSUITE_CONSUMER_SECRET) . '&' . urlencode(NETSUITE_TOKEN_SECRET);
    $signature = base64_encode(hash_hmac("sha1", $base_string, $sig_string, true));

    $auth_header = "OAuth " . 'oauth_signature="' . rawurlencode($signature) . '", ' . 'oauth_version="' . rawurlencode($oauth_version) . '", ' . 'oauth_nonce="' . rawurlencode($oauth_nonce) . '", ' . 'oauth_signature_method="' . rawurlencode($oauth_signature_method) . '", ' . 'oauth_consumer_key="' . rawurlencode(NETSUITE_CONSUMER_KEY) . '", ' . 'oauth_token="' . rawurlencode(NETSUITE_TOKEN_ID) . '", ' . 'oauth_timestamp="' . rawurlencode($oauth_timestamp) . '", ' . 'realm="' . rawurlencode(NETSUITE_ACCOUNT) . '"';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, NETSUITE_URL . '?&script=' . NETSUITE_SCRIPT_ID . '&deploy=' . NETSUITE_DEPLOY_ID);
    curl_setopt($ch, CURLOPT_POST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: ' . $auth_header, 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) ]);

    $content = curl_exec($ch);
    print_r($content);
    curl_close($ch);

}
?>

But I’m getting the following error :

{"error" : {"code" : "INVALID_LOGIN_ATTEMPT", "message" : "Invalid login attempt."}}

How can I solve this?

Advertisement

Answer

You missed account id in the url

    curl_setopt($ch, CURLOPT_URL, NETSUITE_URL . '?&script=' . NETSUITE_SCRIPT_ID . '&deploy=' . NETSUITE_DEPLOY_ID . '&realm=' . NETSUITE_ACCOUNT );
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement