Last active
February 17, 2024 06:47
-
-
Save W3BGUY/fc0be7d03c3ce9a947ffc3ea68ba4671 to your computer and use it in GitHub Desktop.
Basic PHP NetSuite OAuth1.0 (SHA256) authentication to call RESTlet script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
?> |
I get
"""
error code: INVALID_LOGIN_ATTEMPT
error message: Invalid login attempt.
"""
Any other solution INVALID_LOGIN_ATTEMPT error?
This worked for me.
Finally found something that worked.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
giving invalid login attempt