Hi Jeffrey,
You might need to pass authorization as part of header. I tried with below
script and it worked.
Also, you will need to use get method for this API request.
$encodedCreds =
[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("
*someuser:somepassword*"))
$basicAuthValue = "Basic $encodedCreds"
$headers = New-Object
"System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $basicAuthValue)
$headers.Add('Accept', 'application/vnd.go.cd.v1+json')
$response = Invoke-WebRequest -Uri "
http://localhost:8153/go/api/current_user" -Headers $headers -Method *get*;
Write-Host $response
On Fri, Mar 9, 2018 at 11:01 AM, Jeffrey Hales <[email protected]>
wrote:
> I amI am trying to using powershell scripting to access the API. I have
> tried a couple ways (example 1, 2 below) . Both result in same message "You
> are not authorized to access this resource!" I know that my User has admin
> rights to the goserver. API documentation has the following example A.
> Is anyone else using the API via powershell scripting ?
> What I am doing wrong?
>
> Example A:
>
> $ curl 'https://ci.example.com/go/api/current_user' \
> -u 'username:password' \
> -H 'Accept: application/vnd.go.cd.v1+json' \
>
> Example 1:
>
> $headers = New-Object "System.Collections.Generic.
> Dictionary[[String],[String]]"
> $headers.Add('Accept', 'application/vnd.go.cd.v1+json')
> $Body = @{
> User = 'someuser'
> password = 'somepassword'
> }
> $response = Invoke-RestMethod -Uri "https://goserver:8154/go/api/
> current_user" -Headers $headers -Body $Body -Method post;
>
> Example 2:
> $headers = New-Object "System.Collections.Generic.
> Dictionary[[String],[String]]"
> $headers.Add('Accept', 'application/vnd.go.cd.v1+json')
> $secpasswd = ConvertTo-SecureString 'somepassword' -AsPlainText -Force
> $mycreds = New-Object System.Management.Automation.PSCredential
> ('someuser', $secpasswd)
>
> $response = Invoke-RestMethod -Uri "https://goserver:8154/go/api/
> current_user" -Headers $headers -Credential $mycreds -Method post;
>
> --
> You received this message because you are subscribed to the Google Groups
> "go-cd" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"go-cd" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.