Downloading JSON file using Invoke-WebRequest by passing credentials and SessionToken

RajivIyer 96 Reputation points
2024-08-02T05:58:43.4533333+00:00

I am trying to download a JSON file from a URL which requires UserName, Password, User ID and SessionToken. I am referring an article: https://support.tidepool.org/hc/en-us/articles/360029368992-Downloading-your-Tidepool-Web-Notes

PS C:\Users\rajiv\OneDrive\Documents\Personal\Diabetes> Get-Credential | Invoke-WebRequest -Uri https://api.tidepool.org/auth/login -Method Post

I am getting the below error. I am pretty sure that I am passing the correct credentials:

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Invoke-WebRequest : {"code":400,"reason":"Missing id and/or password"}
At line:1 char:18
+ ... redential | Invoke-WebRequest -Uri https://api.tidepool.org/auth/logi ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,918 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,786 Reputation points
    2024-08-02T19:14:03.4+00:00

    Have a look at the Invoke-Credential help. The "Credential" parameter is a named parameter and the cmdlet does not accept pipelined input for it.

    This should accomplish what you're trying to do:

    Invoke-WebRequest -Credential (Get-Credential) -Uri https://api.tidepool.org/auth/login -Method Post

    I think this, too, will work (the credential is passed as an object and the object has a property named 'credential' (which matches the name of the cmdlets parameter) with an appropriate value.

    [PSCustomObject]@{Credential = (Get-Credential)} | Invoke-WebRequest -Uri https://api.tidepool.org/auth/login -Method Post

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.