Need Help to resolve the GET request using Powershell

powershell

#1

I get the below error and I am unable to figure out why… The script works like a magic for POST method, but not able to figure out this doesn’t work for the GET method.

 Invoke-RestMethod : {"errors":[{"code":32,"message":"Could not authenticate you."}]}
 At C:\Users\jodi\Documents\REST\gettweets.ps1:81 char:9
  +         Invoke-RestMethod -URI $HttpEndPoint -Method Get -Headers @{  ...
  +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke- 
   RestMethod], WebException
  + FullyQualifiedErrorId : 
    WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Below is the powershell script that is being used.

        ## Generate a random 32-byte string. I'm using the current time (in seconds) and appending 5 chars to the end to get to 32 bytes 
        ## Base64 allows for an '=' but Twitter does not.  If this is found, replace it with some alphanumeric character 
        $OauthNonce = [System.Convert]::ToBase64String(([System.Text.Encoding]::ASCII.GetBytes("$([System.DateTime]::Now.Ticks.ToString())12345"))).Replace('=', 'g') 
        Write-Host "Generated Oauth none string '$OauthNonce'" 
         
        ## Find the total seconds since 1/1/1970 (epoch time) 
        $EpochTimeNow = [System.DateTime]::UtcNow - [System.DateTime]::ParseExact("01/01/1970", "dd/MM/yyyy", $null) 
        Write-Host "Generated epoch time '$EpochTimeNow'" 
        
        $OauthTimestamp = [System.Convert]::ToInt64($EpochTimeNow.TotalSeconds).ToString(); 
        Write-Host "Generated Oauth timestamp '$OauthTimestamp'" 
         
        ## Build the signature 
        $SignatureBase = "$([System.Uri]::EscapeDataString($HttpEndPoint))&" 
        
        
        $SignatureParams = @{ 
            'oauth_consumer_key' = $ApiKey; 
            'oauth_nonce' = $OauthNonce; 
            'oauth_signature_method' = 'HMAC-SHA1'; 
            'oauth_timestamp' = $OauthTimestamp; 
            'oauth_token' = $AccessToken; 
            'oauth_version' = '1.0'; 
        }
        $Message = "test message"
        $Username = "quasiwritist"
        #$SignatureParams.status = $Message
        $SignatureParams.screen_name = $Username
        $SignatureParams.count = 2
     ##Create a string called $SignatureBase that joins all URL encoded 'Key=Value' elements with a & 
        ## Remove the URL encoded & at the end and prepend the necessary 'POST&' verb to the front 
        "sorting starts"
        $SignatureParams.GetEnumerator() | sort name | foreach {  
            Write-Host "Adding '$([System.Uri]::EscapeDataString(`"$($_.Key)=$($_.Value)&`"))' to signature string" 
            $SignatureBase += [System.Uri]::EscapeDataString("$($_.Key)=$($_.Value)&".Replace(',','%2C').Replace('!','%21')) 
        }             
        $SignatureBase = $SignatureBase.TrimEnd('%26') 
        $SignatureBase = 'GET&' + $SignatureBase 
        Write-Host "Base signature generated '$SignatureBase'" 
        ## Create the hashed string from the base signature 
        $SignatureKey = [System.Uri]::EscapeDataString($ApiSecret) + "&" + [System.Uri]::EscapeDataString($AccessTokenSecret);              
        $hmacsha1 = new-object System.Security.Cryptography.HMACSHA1; 
        $hmacsha1.Key = [System.Text.Encoding]::ASCII.GetBytes($SignatureKey); 
        $OauthSignature = [System.Convert]::ToBase64String($hmacsha1.ComputeHash([System.Text.Encoding]::ASCII.GetBytes($SignatureBase))); 
        Write-Host "Using signature '$OauthSignature'"              
        ## Build the authorization headers using most of the signature headers elements.  This is joining all of the 'Key=Value' elements again 
        ## and only URL encoding the Values this time while including non-URL encoded double quotes around each value 
        $AuthorizationParams = $SignatureParams 
        $AuthorizationParams.Add('oauth_signature', $OauthSignature) 

         $AuthorizationString = 'OAuth ' 
        $AuthorizationParams.GetEnumerator() | sort name | foreach { $AuthorizationString += $_.Key + '="' + [System.Uri]::EscapeDataString($_.Value) + '", ' } 
        $AuthorizationString = $AuthorizationString.TrimEnd(', ') 
        Write-Host "Using authorization string '$AuthorizationString'" 
    
  ##$AuthorizationString = Get-OAuthAuthorization -TweetMessage $Message -HttpEndPoint $HttpEndPoint 
     
    ## Convert the message to a Byte array 
    #$Body = [System.Text.Encoding]::ASCII.GetBytes("status=$Message"); 
    $Body = "status=$Message" 
    Write-Host "Using POST body '$Body'"
    Invoke-RestMethod -URI $HttpEndPoint -Method Get -Headers @{ 'Authorization' = $AuthorizationString } -ContentType "application/x-www-form-urlencoded"