Home‎ > ‎

Powershell .NET Sample

Windows Powershell is scripting environment made by Microsoft. It comes automatically with Windows 7, but you can download it here. The following script demonstrates how to access the Eligiblity service using 2-legged OAuth in Powershell.

Once you've configured your Powershell environment (a good Getting Started guide is here), you can begin running scripts. Create a new script file called GetSchedOfBen-Oauth.ps1 and paste the following code into it.

#####################################################
###
### Enter these values
###
$oauth_consumer_key = "PowershellDemo"
$oauth_consumer_secret = "abc"
$oauth_token = "1VxUb0qYUoluC9ABkpVEaveA7sA="
$oauth_token_secret = "abc"
$instanceid = "354F8E32-C7C0-4C5E-9A30-33CF1EF154CE"
$base_url = "http://preview.healthx.com/api/EligibilityV1/GetScheduleOfBenefits"
$orgid = "E180F61A-AFAF-43CE-A495-28C942854CD4"
$benefit_code = "Badger"
$url = $base_url + "/" + $orgid + "/" + $benefit_code


### .NET's UrlEncode uses lowercase escape codes. We need uppercase ones.
[Reflection.Assembly]::LoadWithPartialName("System.Web") > $null
Function UrlEncode($s)
{
    [System.Web.HttpUtility]::UrlEncode($s).Replace("%2a","%2A").Replace("%2b","%2B").Replace("%2c","%2C").Replace("%2d","%2D").Replace("%2e","%2E").Replace("%2f","%2F").Replace("%3a","%3A").Replace("%3b","%3B").Replace("%3c","%3C").Replace("%3d","%3D").Replace("%3e","%3E").Replace("%3f","%3F")
}

### Compute timestamp
$epoch = New-Object -type DateTime(1970, 1, 1)
$oauth_timestamp = [int] ([System.DateTime]::Now.ToUniversalTime() - $epoch).TotalSeconds

### Create nonce
$random = New-Object -type Random
$oauth_nonce = $random.Next()

### Compute signature
$hasher = New-Object Security.Cryptography.HMACSHA1
$hasher.Key = [Text.Encoding]::ASCII.GetBytes((UrlEncode($oauth_consumer_secret)) + "&" + (UrlEncode($oauth_token_secret)))
$basestring = "GET&" +
              (UrlEncode($url.ToLower())) + "&" +
              (UrlEncode("oauth_consumer_key=" + (UrlEncode($oauth_consumer_key)) + "&oauth_nonce=" + $oauth_nonce + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + $oauth_timestamp + "&oauth_token=" + (UrlEncode($oauth_token)) + "&oauth_version=1.0"))
$signaturebytes = $hasher.ComputeHash([Text.Encoding]::ASCII.GetBytes($basestring))
$signature = [System.Convert]::ToBase64String($signaturebytes)

### Send the request``
$req = [System.Net.WebRequest]::Create($url)
$req.Method = "GET"
$req.Timeout = "3000"
$req.Headers.Add("Authorization","OAuth oauth_token=""" + (UrlEncode($oauth_token)) + """,oauth_consumer_key=""" + (UrlEncode($oauth_consumer_key)) + """,oauth_nonce=""" + $oauth_nonce + """,oauth_signature_method=""HMAC-SHA1"",oauth_signature=""" + (UrlEncode($signature)) + """,oauth_version=""1.0"",oauth_timestamp=""" + $oauth_timestamp + """")
$req.Headers.Add("instanceid", $instanceid)

### Get the response
trap [Exception] {
    ($_.Exception.Message)
    $respstream = new-object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
    $respstream.ReadToEnd().Trim()
}
$resp = $req.GetResponse()
if ($resp -ne $null)
{
    $respstream = new-object System.IO.StreamReader($resp.GetResponseStream())
    $xml = [xml] $respstream.ReadToEnd().Trim()
    $xml.Save([Console]::Out)
}

 Save this file. Then, in Powershell, run it.

. GetOpenList-Oauth.ps1

A response should return that looks something like this.


<ArrayOfScheduleOfBenefits xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ScheduleOfBenefits>
    <BnftPlan>Badger</BnftPlan>
    <Description>Office Visit Copay - $20</Description>
    <DisplayIndicator>1</DisplayIndicator>
    <DisplayOrder>2</DisplayOrder>
    <ItemId>e2629b9c-5950-481c-85a7-384207e1edfc</ItemId>
    <Label>Office Visit</Label>
    <PayorOrgId>e180f61a-afaf-43ce-a495-28c942854cd4</PayorOrgId>
    <Value>$10</Value>
    <adddatetime>2009-10-05T16:30:06.833</adddatetime>
    <moddatetime>0001-01-01T00:00:00</moddatetime>
  </ScheduleOfBenefits>
  <ScheduleOfBenefits>
    ...
  </ScheduleOfBenefits>
  <ScheduleOfBenefits>
    ...
  </ScheduleOfBenefits>
</ArrayOfScheduleOfBenefits>


Troubleshooting