This topic contains 2 replies, has 0 voices, and was last updated by ironside 7 years, 6 months ago.

  • Author
    Posts
  • #6195

    CyberGoo

    I have worked long and hard on this, enjoy.

    ################################################## #################

    #

    # Sample_Netsuite_OAuth Script Using PowerShell.

    # Created by Bob Caputo and Joshua Tellander.

    # Use and revise as needed.

    # No warranty or license is included.

    # We will attempt to answer questions,

    # we do not guarantee answers in a timely

    # manner if at all.

    #

    # NetSuite declined to help because it is a

    # PowerShell script and therefore

    # “out of our scope”, per service case.

    #

    # Have fun!!!!!!!

    #

    ################################################## #################

    [Reflection.Assembly]::LoadWithPartialName(“System.Security”)

    [Reflection.Assembly]::LoadWithPartialName(“System.Net”)

    [Reflection.Assembly]::LoadWithPartialName(“System.Web”)

    [Reflection.Assembly]::LoadWithPartialName(“System.IO”)

    $oauth_consumer_secret = “xxx”;

    $oauth_token_secret = “xxx”;

    $oauth_consumer_key=”xxx”

    $oauth_token=”xxx”

    $oauth_signature_method=”HMAC-SHA1″

    $oauth_version=”1.0″

    $HTTP_method = “GET”

    $realm = “xxxxxxx”

    #$base_url = “https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl”

    #$full_url = “https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl?script=271&deploy=1″

    $oauth_nonce = ”

    $oauth_timestamp = ”

    $oauth_nonce = Get-Random -Minimum 123400 -Maximum 9999999

    $oauth_timestamp = [int64](([datetime]::UtcNow)-(get-date “1/1/1970”)).TotalSeconds

    ###

    ## Create oauth_signature string, order is extremely important.

    ## The ampersands are important too, string syntax is too.

    ###

    $base_string = “deploy=1” + “&”;

    $base_string += “oauth_consumer_key=” + $oauth_consumer_key + “&”;

    $base_string += “oauth_nonce=” + $oauth_nonce + “&”;

    $base_string += “oauth_signature_method=HMAC-SHA1” + “&”;

    $base_string += “oauth_timestamp=” + $oauth_timestamp + “&”;

    $base_string += “oauth_token=” + $oauth_token + “&”;

    $base_string += “oauth_version=” + $oauth_version + “&”;

    $base_string += “script=271”;

    Write-Host -BackgroundColor:Yellow -ForegroundColor:Blue “Sig Base Str: $base_string”

    Write-Host ” ”

    ###

    ## URL-Encode the base string.

    ## A bit much but it works for OAuth 1.0

    ###

    $base_string = [System.Web.HttpUtility]::UrlEncode($base_string).Replace(“%2a”,”%2A”).Rep lace(“%2b”,”%2B”).Replace(“%2c”,”%2C”).Replace(“%2 d”,”%2D”).Replace(“%2e”,”%2E”).Replace(“%2f”,”% 2F” ).Replace(“%3a”,”%3A”).Replace(“%3b”,”%3B”).Replac e(“%3c”,”%3C”).Replace(“%3d”,”%3D”).Replace(“%3e “, “%3E”).Replace(“%3f”,”%3F”)

    Write-Host -BackgroundColor:White -ForegroundColor:Red “Base 3: $base_string”

    Write-Host ” ”

    ###

    ## Concatenate the result HTTP method, the URL-encoded Base URL and the result value using “&”

    ###

    $url_base = “https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl”

    $url_base = [System.Web.HttpUtility]::UrlEncode($url_base).Replace(“%2a”,”%2A”).Replac e(“%2b”,”%2B”).Replace(“%2c”,”%2C”).Replace(“%2d “, “%2D”).Replace(“%2e”,”%2E”).Replace(“%2f”,”%2F “).R eplace(“%3a”,”%3A”).Replace(“%3b”,”%3B”).Replace(” %3c”,”%3C”).Replace(“%3d”,”%3D”).Replace(“%3e”,”% 3 E”).Replace(“%3f”,”%3F”)

    Write-Host -BackgroundColor:White -ForegroundColor:Red “URL Base: $url_base”

    Write-Host ” ”

    $base_string = $HTTP_method + “&” + $url_base + “&” + $base_string

    Write-Host -BackgroundColor:White -ForegroundColor:Red “Base String: $base_string”

    Write-Host ” ”

    ###

    ## Sign the result string using the consumer secret and token secret concatenated using “&”

    ###

    $key = $oauth_consumer_secret + “&” + $oauth_token_secret;

    Write-Host -BackgroundColor:White -ForegroundColor Blue “Step 6.1: $key”

    ###

    ## Now encrypt the oauth signature.

    ###

    $hmacsha1 = new-object System.Security.Cryptography.HMACSHA1;

    $hmacsha1.Key = [System.Text.Encoding]::ASCII.GetBytes($key);

    $oauth_signature = [System.Convert]::ToBase64String($hmacsha1.ComputeHash([System.Text.Encoding]::ASCII.GetBytes($base_string)));

    Write-Host -BackgroundColor:White -ForegroundColor Blue “OAuth Sig: $oauth_signature”

    Write-Host ” ”

    ###

    ## URL encode the oauth_signature

    ###

    $oauth_signature = [System.Web.HttpUtility]::UrlEncode($oauth_signature)

    Write-Host -BackgroundColor:White -ForegroundColor:Blue “URL Encode Sig: $oauth_signature”

    Write-Host ” ”

    ###

    ## Create Header, Order and quotes are very important.

    ###

    $header = ” OAuth “;

    $header += “oauth_signature=`”” + $oauth_signature + “`”,”;

    $header += “oauth_version=`”” + “1.0`”,”;

    $header += “oauth_nonce=`”” + $oauth_nonce + “`”,”;

    $header += “oauth_signature_method=`”” + “HMAC-SHA1`”,”;

    $header += “oauth_consumer_key=`”” + $oauth_consumer_key + “`”,”;

    $header += “oauth_token=`”” + $oauth_token + “`”,”;

    $header += “oauth_timestamp=`”” + $oauth_timestamp + “`”,”;

    $header += “realm=`”” + “1234567`””;

    Write-Host -BackgroundColor:White -ForegroundColor:Blue “Header: $header”

    Write-Host ” ”

    try {

    ###

    ## This is were the action happens.

    ###

    [System.Net.HttpWebRequest] $r = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create(“https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl?script=271&deploy=1”)

    $r.Method = “GET”

    $r.ContentType = “application/x-www-form-urlencoded”;

    $r.Headers.Add(“Authorization”, $header);

    $hedStr = $r.Headers.GetValues(“Authorization”);

    [System.Net.WebResponse] $resp = $r.GetResponse();

    $rs = $resp.GetResponseStream();

    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;

    [string] $results = $sr.ReadToEnd();

    } catch [System.Net.WebException] {

    Write-Warning $_

    if ($_.response)

    {

    $_.response.Close();

    }

    }

    finally

    {

    if ($sr)

    {

    $sr.close();

    $sr.dispose();

    }

    if ($resp)

    {

    $resp.close();

    }

    }

    $results;
    This is a cached copy. Click here to see the original post.

  • #6196

    srodriguez

    This helped a lot. thank you sir!

  • #6197

    ironside

    Note that ‘no license’ typically means nobody can use it….

You must be logged in to reply to this topic.