ForumCategory: OtherCan’t call api with query and body parameters
DEEPAK TOMAR asked 49 years ago

Hi,
 
I am trying to retrieve data using powershell script but there is a limit of 50 interaction history per request hence I need to use the url parameters but it doesn’t seems to be working.
Please have look at the code and the information provided on your site, don’t understand why its not working.
 
cls hist
$oauth_consumer_key=’…’
$oauth_consumer_secret=’…’
$oauth_token=’…’
$oauth_token_secret=’…’
$url=”https://va.enghist.liveperson.net/interaction_history/api/account/37007926/interactions/search”
#$url= “https://va.enghist.liveperson.net/interaction_history/api/account/37007926/interactions/search?limit=50&offset=0” #Not working
$body=@{“ended”= ‘ true’ ; “start”= @{ “from” = 1531180800000; “to”=1531699200000 } }
$oauth_nonce = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([System.DateTime]::Now.Ticks.ToString()));
$ts = [System.DateTime]::UtcNow – [System.DateTime]::ParseExact(“01/01/1970”, “dd/MM/yyyy”, $null).ToUniversalTime();
$oauth_timestamp = [System.Convert]::ToInt64($ts.TotalSeconds).ToString();
$signature = “POST&”;
$signature += [System.Uri]::EscapeDataString( $url) + “&”;
$signature += [System.Uri]::EscapeDataString(“oauth_consumer_key=” + $oauth_consumer_key + “&”);
$signature += [System.Uri]::EscapeDataString(“oauth_nonce=” + $oauth_nonce + “&”);
$signature += [System.Uri]::EscapeDataString(“oauth_signature_method=HMAC-SHA1&”);
$signature += [System.Uri]::EscapeDataString(“oauth_timestamp=” + $oauth_timestamp + “&”);
$signature += [System.Uri]::EscapeDataString(“oauth_token=” + $oauth_token + “&”);
$signature += [System.Uri]::EscapeDataString(“oauth_version=1.0”);

$signature_key = [System.Uri]::EscapeDataString($oauth_consumer_secret) + “&” + [System.Uri]::EscapeDataString($oauth_token_secret);

$hmacsha1 = new-object System.Security.Cryptography.HMACSHA1;
$hmacsha1.Key = [System.Text.Encoding]::ASCII.GetBytes($signature_key);
$oauth_signature = [System.Convert]::ToBase64String($hmacsha1.ComputeHash([System.Text.Encoding]::ASCII.GetBytes($signature)));

$oauth_authorization = ‘OAuth ‘;
$oauth_authorization += ‘oauth_consumer_key=”‘ + [System.Uri]::EscapeDataString($oauth_consumer_key) + ‘”,’;
$oauth_authorization += ‘oauth_nonce=”‘ + [System.Uri]::EscapeDataString($oauth_nonce) + ‘”,’;
$oauth_authorization += ‘oauth_signature=”‘ + [System.Uri]::EscapeDataString($oauth_signature) + ‘”,’;
$oauth_authorization += ‘oauth_signature_method=”HMAC-SHA1″,’
$oauth_authorization += ‘oauth_timestamp=”‘ + [System.Uri]::EscapeDataString($oauth_timestamp) + ‘”,’
$oauth_authorization += ‘oauth_token=”‘ + [System.Uri]::EscapeDataString($oauth_token) + ‘”,’;
$oauth_authorization += ‘oauth_version=”1.0″‘;
$headers=@{‘Authorization’= $oauth_authorization}
$data=$body |ConvertTo-Json
Invoke-restmethod -Method post -Uri $url -Body $data -Headers $headers -ContentType ‘application/JSON’
 

Mark Manguno replied 12 months ago

Hi @DEEPAK TOMAR, I have edited your question to remove your api keys. We strongly suggest not sharing your credentials on a public forum.

1 Answers
Mark Manguno answered 12 months ago

Hi @DEEPAK TOMAR,
When building your OAuth signature you cannot include the queryparams in the URL. This is why it is failing. Append the queryparams to the URL after building the OAuth signature.

伊藤麻里奈 replied 2 months ago

Hi @Mark Manguno,
The same happened to me.(powershell)
Please give me specific examples. of how to add a URLparameter to
“Invoke-restmethod -Method post -Uri $ url -Body $ data -Headers $ headers -ContentType ‘application / JSON’ ”
in the above code.