Skip to content

Instantly share code, notes, and snippets.

@W3BGUY
Last active February 17, 2024 06:47
Show Gist options
  • Save W3BGUY/fc0be7d03c3ce9a947ffc3ea68ba4671 to your computer and use it in GitHub Desktop.
Save W3BGUY/fc0be7d03c3ce9a947ffc3ea68ba4671 to your computer and use it in GitHub Desktop.
Basic PHP NetSuite OAuth1.0 (SHA256) authentication to call RESTlet script.
<?php
/**
* @Author w3bguy
* @Created 2021-09-08
*
* Description:
* Basic PHP NetSuite OAuth1.0 (SHA256) authentication to call RESTlet script.
*
* @modifications
* Date Author Version Remarks
* 2021-09-08 w3bguy v21.9.8-1 Created
*
*/
if(!defined('NETSUITE_DEPLOYMENT_URL')){
define("NETSUITE_DEPLOYMENT_URL",'https://[NS Account Number].restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=[NS Script ID]&deploy=[NS Deploy ID]');
}
if(!defined('NETSUITE_URL')){
define("NETSUITE_URL",'https://[NS Account Number].restlets.api.netsuite.com');
}
if(!defined('NETSUITE_REST_URL')){
define("NETSUITE_REST_URL",'https://[NS Account Number].restlets.api.netsuite.com/app/site/hosting/restlet.nl');
}
if(!defined('NETSUITE_SCRIPT_ID')){
define("NETSUITE_SCRIPT_ID",'[NS Script ID]');
}
if(!defined('NETSUITE_DEPLOY_ID')){
define("NETSUITE_DEPLOY_ID",'[NS Deploy ID]');
}
if(!defined('NETSUITE_ACCOUNT')){
define("NETSUITE_ACCOUNT",'[NS Account Number]');
}
if(!defined('NETSUITE_CONSUMER_KEY')){
define("NETSUITE_CONSUMER_KEY",'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
}
if(!defined('NETSUITE_CONSUMER_SECRET')){
define("NETSUITE_CONSUMER_SECRET",'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
}
if(!defined('NETSUITE_TOKEN_ID')){
define("NETSUITE_TOKEN_ID",'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
}
if(!defined('NETSUITE_TOKEN_SECRET')){
define("NETSUITE_TOKEN_SECRET",'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
}
$payload='';
$encoded=json_encode($payload);
$data_string=$encoded;
$oauth_nonce=md5(mt_rand());
$oauth_timestamp=time();
$oauth_signature_method='HMAC-SHA256';
$oauth_version="1.0";
$base_string=
"POST&".urlencode(NETSUITE_REST_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
."&script=".NETSUITE_SCRIPT_ID
);
$key=rawurlencode(NETSUITE_CONSUMER_SECRET).'&'.rawurlencode(NETSUITE_TOKEN_SECRET);
$signature=base64_encode(hash_hmac("sha256",$base_string,$key,true));
$auth_header='OAuth '
.'realm="'.rawurlencode(NETSUITE_ACCOUNT).'",'
.'oauth_consumer_key="'.rawurlencode(NETSUITE_CONSUMER_KEY).'",'
.'oauth_token="'.rawurlencode(NETSUITE_TOKEN_ID).'",'
.'oauth_signature_method="'.rawurlencode($oauth_signature_method).'",'
.'oauth_timestamp="'.rawurlencode($oauth_timestamp).'",'
.'oauth_nonce="'.rawurlencode($oauth_nonce).'",'
.'oauth_version="'.rawurlencode($oauth_version).'",'
.'oauth_signature="'.rawurlencode($signature).'"';
$curl=curl_init();
curl_setopt_array($curl,array(
CURLOPT_URL=>NETSUITE_DEPLOYMENT_URL,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_ENCODING=>'',
CURLOPT_MAXREDIRS=>10,
CURLOPT_TIMEOUT=>0,
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_HTTP_VERSION=>CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST=>'POST',
CURLOPT_POSTFIELDS=>$data_string,
CURLOPT_HTTPHEADER=>array(
'Authorization: '.$auth_header,
'Content-Type: text/plain'
),
));
$reply=curl_exec($curl);
curl_close($curl);
if($reply){
echo "\r\n->".$reply."<-\r\n";
}
?>
@AVSS008
Copy link

AVSS008 commented Aug 7, 2022

giving invalid login attempt

@ettedo2000
Copy link

I get
"""
error code: INVALID_LOGIN_ATTEMPT
error message: Invalid login attempt.
"""

@conflicker
Copy link

Any other solution INVALID_LOGIN_ATTEMPT error?

@abdulhaq
Copy link

This worked for me.

@webcyst-gursahb
Copy link

Finally found something that worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment