CSharp · Console
using System;
using RestSharp;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
Console.WriteLine("starting");
NetSuiteClient nsClient = new NetSuiteClient();
RestRequest request = nsClient.buildClient();
RestClient client = nsClient._client;
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Console.WriteLine("ending");
}
}
public class NetSuiteClient
{
public partial class TokenPassport
{
private string algorithmField;
private string valueField;
public string algorithm
{
get
{
return this.algorithmField;
}
set
{
this.algorithmField = value;
}
}
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
public RestClient _client;
string _endpoint = "https://11111111.restlets.api.netsuite.com";
string resource = "/app/site/hosting/restlet.nl";
Method method = RestSharp.Method.GET;
string script = "1";
string deploy = "1";
string consumerKey = "abc123";
string consumerSecret = "abc123";
string tokenId = "abc123";
string tokenSecret = "abc123";
string realm = "11111111";
public RestRequest buildClient()
{
_client = new RestClient(_endpoint);
RestRequest request = new RestRequest(resource, method);
if (method == RestSharp.Method.POST)
{
request.AddJsonBody(new
{
A = "foo", B = "bar"
}
);
}
//Compute the nonce
string nonce = computeNonce();
//Compute the timestamp
long timestamp = computeTimestamp();
TokenPassport sign = computeSignature(deploy, consumerKey, consumerSecret, tokenId, tokenSecret, nonce, timestamp, script, method.ToString(), resource);
string auth = buildAuth(sign.Value, nonce, sign.algorithm, consumerKey, tokenId, timestamp, realm);
request.AddQueryParameter("script", script);
request.AddQueryParameter("deploy", deploy);
request.AddParameter("Authorization", auth, ParameterType.HttpHeader);
return request;
}
public TokenPassport computeSignature(string deploy, string consumerKey, string consumerSecret, string tokenId, string tokenSecret, string nonce, long timestamp, string script, string method, string resource)
{
string baseString = "deploy=" + deploy + "&" + "oauth_consumer_key=" + consumerKey + "&" + "oauth_nonce=" + nonce + "&" + "oauth_signature_method=HMAC-SHA256&" + "oauth_timestamp=" + timestamp.ToString() + "&" + "oauth_token=" + tokenId + "&" + "oauth_version=1.0&" + "script=" + script;
string baseStringEncode = System.Net.WebUtility.UrlEncode(baseString);
string fullURL = method + "&" + System.Net.WebUtility.UrlEncode(_endpoint + resource) + "&" + baseStringEncode;
string key = consumerSecret + "&" + tokenSecret;
string signature = "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyBytes = encoding.GetBytes(key);
byte[] baseStringBytes = encoding.GetBytes(fullURL);
using (var hmacSha256 = new HMACSHA256(keyBytes))
{
byte[] hashBaseString = hmacSha256.ComputeHash(baseStringBytes);
signature = Convert.ToBase64String(hashBaseString);
}
string signatureEncode = System.Net.WebUtility.UrlEncode(signature);
TokenPassport sign = new TokenPassport();
sign.algorithm = "HMAC-SHA256";
sign.Value = signatureEncode;
return sign;
}
public string buildAuth(string value, string nonce, string algorithm, string consumerKey, string tokenId, long timestamp, string realm)
{
string auth = @"OAuth oauth_signature=""" + value + @""",oauth_version=""" + "1.0" + @""",oauth_nonce=""" + nonce + @""",oauth_signature_method=""" + algorithm + @""",oauth_consumer_key=""" + consumerKey + @""",oauth_token=""" + tokenId + @""",oauth_timestamp=""" + timestamp.ToString() + @""",realm=""" + realm + "\"";
return auth;
}
public string computeNonce()
{
//Use System.Security.Cryptography for RNGCryptoServiceProvider
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] data = new byte[20];
rng.GetBytes(data);
int value = Math.Abs(BitConverter.ToInt32(data, 0));
return value.ToString();
}
public long computeTimestamp()
{
return ((long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
}
}