我尝试使用PHP Toolkit通过帐户ID,用户名和密码访问Netsuite。我可以使用上述凭据获取客户详细信息。我有访问令牌。但是现在我尝试使用访问令牌访问Netsuite。我需要PHP代码才能使用令牌访问Netsuite,我想获取客户详细信息。
提前致谢
答案 0 :(得分:10)
以下是我为PHP编写的一些代码 - >使用Restlets和OAuth进行NS集成:
define("NETSUITE_URL", 'https://rest.netsuite.com/app/site/hosting/restlet.nl');
define("NETSUITE_SCRIPT_ID", 'XXXXXX');
define("NETSUITE_DEPLOY_ID", 'XXXXXX');
define("NETSUITE_ACCOUNT", 'XXXXXX');
define("NETSUITE_CONSUMER_KEY", 'XXXXXX');
define("NETSUITE_CONSUMER_SECRET", 'XXXXXX');
define("NETSUITE_TOKEN_ID", 'XXXXXX');
define("NETSUITE_TOKEN_SECRET", 'XXXXXX');
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 . '&realm=' . NETSUITE_ACCOUNT);
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)
]);
curl_exec($ch);
curl_close($ch);
}
答案 1 :(得分:1)
Netsuite TBA Restlets 在 2021.1 版本中将不再支持 SHA1,您必须将 PHP 代码更改为 来自 $oauth_signature_method = 'HMAC-SHA1';到 $oauth_signature_method = 'HMAC-SHA256'; 和 from $signature = base64_encode(hash_hmac("sha1", $base_string, $sig_string, true));到 $signature = base64_encode(hash_hmac("sha256", $base_string, $sig_string, true));
define("NETSUITE_URL", 'https://rest.netsuite.com/app/site/hosting/restlet.nl');
define("NETSUITE_SCRIPT_ID", 'XXXXXX');
define("NETSUITE_DEPLOY_ID", 'XXXXXX');
define("NETSUITE_ACCOUNT", 'XXXXXX');
define("NETSUITE_CONSUMER_KEY", 'XXXXXX');
define("NETSUITE_CONSUMER_SECRET", 'XXXXXX');
define("NETSUITE_TOKEN_ID", 'XXXXXX');
define("NETSUITE_TOKEN_SECRET", 'XXXXXX');
function sendOrderToNS($details) {
$data_string = json_encode($details);
$oauth_nonce = md5(mt_rand());
$oauth_timestamp = time();
$oauth_signature_method = 'HMAC-SHA256';
$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("sha256", $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 . '&realm=' . NETSUITE_ACCOUNT);
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)
]);
curl_exec($ch);
curl_close($ch);
}
答案 2 :(得分:0)
我会发表评论,但目前无法发表评论。 michoel的回答是正确的,但是当'realm = ...'是$ auth_header变量的一部分时,抛出了“无效的登录尝试”错误。取出该字符串可消除此问题。