Santosh Dhanasure
Santosh Dhanasure

Reputation: 1013

Invoke-WebRequest doesn't work with token

Im trying to execute ansible tower jobtemplate API to check if the given template ID is valid. However, I am always getting unauthorized status code. Im generating token from ansible tower and sending it to next webreqeust. Below is the code snapshot of what I am trying to do, any clue what I am missing here. Does powershell support token based authentication or is there a different way to set a token in webrequest ?

Any help is much appreciated.

try {
    $json=ConvertTo-Json (@{"username"="*****";"password"="******";})
    $token = (Invoke-WebRequest -Uri "https://www.ansiblet.com/api/v1/authtoken/"  -Body $json -ContentType "application/json" -Method POST).content | ConvertFrom-Json

$headers = @{Authorization="Bearer $token"}

$result = (Invoke-WebRequest -Uri "https://www.ansiblet.com/api/v1/job_templates/{templateId}" -Header $headers -Method GET -ContentType "application/json").content | ConvertFrom-Json

Write-Host $result
}
catch{
    echo $site = $_.Exception.response
}

This web request isn't working.

Response:

Name                           Value                                                                                                       
----                           -----                                                                                                       
Token                          @{token=fcadd3e0326727e14f50401d4b8aaf91dc44ae47; expires=2018-09-14T12:30:11.546Z}                         
Authorization                  Bearer                                                                                                      
=

IsMutuallyAuthenticated : False
Cookies                 : {}
Headers                 : {Transfer-Encoding, Connection, X-API-Time, Allow...}
SupportsHeaders         : True
ContentLength           : -1
ContentEncoding         : 
ContentType             : application/json
CharacterSet            : 
Server                  : nginx/1.10.2
LastModified            : 9/14/2018 5:30:16 PM
StatusCode              : Unauthorized
StatusDescription       : UNAUTHORIZED
ProtocolVersion         : 1.1
ResponseUri             : https://www.ansiblet.com/api/v1/job_templates/1328/
Method                  : GET
IsFromCache             : False

Upvotes: 1

Views: 20846

Answers (3)

Santosh Dhanasure
Santosh Dhanasure

Reputation: 1013

Finally, Invoke-RestMethod did the trick for me.

Invoke-RestMethod $url -Headers @{Authorization="Token $token"}

I'm still looking for the solution using Invoke-WebRequest since my other calls are making use of it. Not sure what is failing, I have tried multiple solutions but did not work.

e.g

$headers = @{Authorization="Token $token"}
$headers = @{Authorization="Bearer $token"}
$headers = @{Authorization="Basic $token"}

Upvotes: 4

Guenther Schmitz
Guenther Schmitz

Reputation: 1999

try this: change the line

 $token = (Invoke-WebRequest -Uri "https://stage-ansible-tower.hc.cloud.uk.hsbc/api/v1/authtoken/"  -Body $json -ContentType "application/json" -Method POST).content | ConvertFrom-Json

to

 $token = ((Invoke-WebRequest -Uri "https://stage-ansible-tower.hc.cloud.uk.hsbc/api/v1/authtoken/"  -Body $json -ContentType "application/json" -Method POST).content | ConvertFrom-Json).token

Upvotes: 0

Vadim
Vadim

Reputation: 126

$token variable contains 2 fields - token and expires. Maybe it needs to be the only one token filed contents (fcadd3e0326727e14f50401d4b8aaf91dc44ae47)?

Upvotes: 0

Related Questions