Re: [go-cd] API Authentication powershell script "You are not authorized to access this resource!"
Thanks that worked On Friday, 9 March 2018 14:49:04 UTC-7, Ankit wrote: > 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 > 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 go-cd+un...@googlegroups.com . >> 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 go-cd+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-cd] API Authentication powershell script "You are not authorized to access this resource!"
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 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 go-cd+unsubscr...@googlegroups.com. > 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 go-cd+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.