I need to add an auth bearer to requests I make to an API. I have done this in C# but need to do it via powershell. I tried turning my C# method to a cmdlet like this:
[Cmdlet(VerbsCommunications.Get, "Token")]
public class GetAuthTokenCommand : Cmdlet
{
// Overide the ProcessRecord method
protected override void ProcessRecord()
{
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/blablaguid/oauth2/token");
Task<AuthenticationResult> resultTask = authContext.AcquireTokenAsync(
"MyResourceUri",
"MyClientId",
new Uri("https://login.live.com/oauth20_desktop.srf"),
new Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters(PromptBehavior.Auto, false));
resultTask.Wait();
WriteObject("Token: "+ resultTask.Result.AccessToken);
}
}
However, this gives me an error:
+ CategoryInfo : NotSpecified: (:) [Send-Greeting], AggregateException
+ FullyQualifiedErrorId : System.AggregateException,GetAuthtoken.SendGreetingCommand
Any ideas?
I've took this sometime ago from somewhere on the web (can't recall where) it works for querying Azure REST\Graph Api.