I'm trying to write a Powershell script so I can get data from a web API that uses 'one-legged' (this means that you only have the consumer key (username) and consumer secret (password) ) OAuth1.0 authentication. I've studied the OAuth1.0 docs (https://oauth.net/core/1.0/) and then produced the following script:
$response = $null
$url = 'https://api.holidaypictures.com/api/v1.0/0'
$oauth_consumer_key = 'myusername@domain.com'
$oauth_consumer_secret = 'SuperSecretHash'
$oauth_nonce = -join ((65..90) + (97..122) | Get-Random -Count 12 | % {[char]$_})
$oauth_signature_method = 'HMAC-SHA1'
$oauth_timestamp = [int64](([datetime]::UtcNow)-(get-date "1/1/1970")).TotalSeconds
$oauth_token = '' # Don't have this because 'one-legged' authentication
$oauth_token_secret = '' # Don't have this because 'one-legged' authentication
$oauth_version = '1.0'
$method = 'POST'
$base_string = 'oauth_consumer_key=' + $oauth_consumer_key
$base_string += '&oauth_nonce=' + $oauth_nonce
$base_string += '&oauth_signature_method=' + $oauth_signature_method
$base_string += '&oauth_timestamp=' + $oauth_timestamp
$base_string += '&oauth_token=' + $oauth_token
$base_string += '&oauth_version=' + $oauth_version
$signature_base_string = $method + '&' + [System.Web.HttpUtility]::UrlEncode($url) + '&' + [System.Web.HttpUtility]::UrlEncode($base_string)
$key = $oauth_consumer_secret + '&' + $oauth_token_secret
$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($signature_base_string)));
$oauth_signature
$oauth_signature = [System.Web.HttpUtility]::UrlEncode($oauth_signature)
$oauth_signature
$auth = 'OAuth '
$auth += 'oauth_consumer_key="' + $oauth_consumer_key + '",'
$auth += 'oauth_nonce="' + $oauth_nonce + '",'
$auth += 'oauth_signature="' + $oauth_signature + '",'
$auth += 'oauth_signature_method="' + $oauth_signature_method + '",'
$auth += 'oauth_timestamp="' + $oauth_timestamp + '",'
$auth += 'oauth_token="' + $oauth_token + '",'
$auth += 'oauth_version="' + $oauth_version + '"'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", $auth)
$body = "{`n `"pageSize`": 10,`n `"sort`": [`n {`n `"key`": `"info.date`",`n `"direction`": -1`n `n }`n ],`n `"idSelection`": [],`n `"query`": []`n}"
$response = Invoke-RestMethod -Method $method -Headers $headers -body $body -Uri $url
When I run this code the following error occurs:
Invoke-RestMethod : {"status":401,"message":"Could not authorize request","scope":"SECURITY","details":null}
At line:47 char:13
+ $response = Invoke-RestMethod -Method $method -Headers $headers -body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Method: POST, Reque\u2026application/json
}:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
In other words: It fails to authenticate. Is there any who can help me out on this one? Would be kindly appreciated!
It's about the MoreApp.com API by the way... Don't know if that helps.