Accessing SalesForce.com using Web Services and Scripting REST– Part 3

 

Powershell – SalesForce.com – REST Example :

CLS

function Get-AuthorizationTokenWithUsernamePasswordFlow ` 
($client_id, $client_secret, $username, $password, $security_token)
{
    Add-Type -AssemblyName System.Web

    # $uri = "https://test.salesforce.com/services/oauth2/token";
    $uri = "https://login.salesforce.com/services/oauth2/token";
    $grant_type = "password";

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

    $requestBody = "";
    $requestBody += "grant_type=$grant_type";
    $requestBody += "&client_id=$client_id";
    $requestBody += "&client_secret=$client_secret";
    $requestBody += "&username=$username";
    $requestBody += "&password=$password$security_token";

    
    Write-Host "Uri:" $uri
    Write-Host "Body:" $requestBody

    Invoke-RestMethod -Method Post -Uri $uri -Body $requestBody
}

# usage create a ClientID and ClientSecret
# https://help.salesforce.com/apex/HTViewSolution?id=000205876&language=en_US

# https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com
# Obtaining a Token in an Autonomous Client (Username and Password Flow)

$Cred = Get-Credential -credential "Email.Address@Company.com"
$username = $Cred.UserName
$password = $Cred.GetNetworkCredential().Password $client_id = "YOUR CLIENT ID";
$client_secret = "YOUR CLIENT SECRET";
$security_token = "YOUR SECURITY TOKEN";

# Debug
 Get-AuthorizationTokenWithUsernamePasswordFlow ` 
 $client_id $client_secret $username $password $security_token

$Token = Get-AuthorizationTokenWithUsernamePasswordFlow ` 
$client_id $client_secret $username $password $security_token

$URI = $Token.instance_url+"/services/data/v20.0/"

$Response = Invoke-RestMethod -URI $URI -Method GET ` 
-Headers @{"Authorization" = "OAuth " + $Token.access_token};

Write-Host ""

# Response = availaible REST services

$Response

Write-Host ""

# Query Accounts Example 
Write-Host "Accounts :"

$StrQuery = "SELECT Name, Id from Account LIMIT 100"

$URI = $Token.instance_url+"/services/data/v20.0/query?q=" + $StrQuery

$Response = Invoke-RestMethod -URI $URI -Method GET ` 
-Headers @{"Authorization" = "OAuth " + $Token.access_token};

Write-Host ""
Write-Host " # of Records : "
$Response.records.Count

Write-Host ""
Write-Host "Enum Account Name :"
$Response.records | % {$_.name}


# Query Opporunities Example 
$StrQuery = "SELECT AccountId, OwnerId, Name, Description, ` 
StageName, Amount, Probability FROM Opportunity WHERE StageName <> `</pre>
<pre>'Closed Lost' ORDER BY StageName "

$URI = $Token.instance_url+"/services/data/v20.0/query?q=" + $StrQuery

$Response = Invoke-RestMethod -URI $URI -Method GET ` 
-Headers @{"Authorization" = "OAuth " + $Token.access_token};

$Response.records.Count

Write-Host ""
Write-Host "Query Output :"

Foreach ($objItem in $Response.records) {

    "{0,-120} {1, 25} {2, 30}" -f `
 
   $objItem.name, $objItem.StageName, $objItem.Amount
 }

This script will return your Access Token, using this function

Get-AuthorizationTokenWithUsernamePasswordFlow

image

Next it will list the REST API’s you can use

image

Query Accounts Example :

image

Enjoy !

Smile

Advertisements

One Response to Accessing SalesForce.com using Web Services and Scripting REST– Part 3

  1. Navan Mehra says:

    Thanks for sharing the details. When trying the same steps you mentioned above, I get 400 Bad Request error when I try to query the api after login. I received the access token and when appending it to header and querying, i receive Bad request error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: