JJENSEN3
asked on
Generating Authentication Token Using Windows PowerShell script
I am, at best, a novice user when it comes to writing Powershell scripts, so any simplified explanation to this question would be appreciated.
I am accessing one of our vendors API services. In order to access it, I need to generate an authentication token. I have the url to send the request to, and I have the username and password. What I am not sure of, is how to write the script to retrieve the token. Is there a "generic" method to do this?
Again, feel free to break it down to the most basic of terms. I won't be offended. :)
Thank you in advance.
I am accessing one of our vendors API services. In order to access it, I need to generate an authentication token. I have the url to send the request to, and I have the username and password. What I am not sure of, is how to write the script to retrieve the token. Is there a "generic" method to do this?
Again, feel free to break it down to the most basic of terms. I won't be offended. :)
Thank you in advance.
You can also try it like this :
The following method allows you to create the token and then post it to an API.
reference : https://vss-wiki.eis.utoronto.ca/display/API/How+to+generate+an+access+token+using+PowerShell
The following method allows you to create the token and then post it to an API.
reference : https://vss-wiki.eis.utoronto.ca/display/API/How+to+generate+an+access+token+using+PowerShell
There isn't really any generic method for this. The information and syntax that you will have to supply in the web request will vary depending on the design of the API, and as such the API documentation is a must to proceed. Anything I provide here is only an example of how things might be used.
Here's a sample showing how I had to do something similar for a particular environment.
Here's a sample showing how I had to do something similar for a particular environment.
# Define a base part of the URL (useful when there may be different environments, e.g. sandbox and production)
$restURLbase = "https://restapi.somedomain.com"
# Get an access token for later calls
$authBody = @{
grant_type = 'password'
username = 'someuser'
password = 's0mePa$s'
}
$authURL = "$restURLbase/authorization"
$auth = Invoke-RestMethod -Uri $authURL -Method Post -Body $authBody
# Make a request using the access token
$headers = @{
Authorization = "$($auth.tokenType) $($auth.accessToken)"
}
$restURL = "$restURLbase/something/blah"
$data = Invoke-RestMethod -Uri $restURL -ContentType 'application/json' -Method Get -Headers $headers
ASKER
Footech,
This is all that I have been given by the API owner:
https://api.xxx.com/xxx/authenticate
Post below request to above URI
{
"username":"oldenkamp",
"password":"<password>"
}
Above request will get you Response as below:
{
"status": "success",
"id_token": "eyJraWQiOiJuZjlzTzhaeENtb XVCNVF2eFJ 0eXE4cU55R GF2OGF2VTB vaWJQeVllc DZvPSIsImF sZyI6IlJTM jU2In0.eyJ zdWIiOiJlM mMzNWI4YS0 0OTU3LTRiM mYtOGI3My0 2NWEyMGEwY WIxZGQiLCJ jb2duaXRvO mdyb3VwcyI 6WyJVc2VyI iwiRGV2ZWx vcGVyIl0sI mVtYWlsX3Z lcmlmaWVkI jp0cnVlLCJ jb2duaXRvO nByZWZlcnJ lZF9yb2xlI joiYXJuOmF 3czppYW06O jkxNTE2NDU 1MjY0NTpyb 2xlXC9BUEl HYXRld2F5R GV2R3JvdXA iLCJpc3MiO iJodHRwczp cL1wvY29nb ml0by1pZHA udXMtZWFzd C0xLmFtYXp vbmF3cy5jb 21cL3VzLWV hc3QtMV9CW W15NGt4SVU iLCJjb2dua XRvOnVzZXJ uYW1lIjoib 2xkZW5rYW1 wIiwiY29nb ml0bzpyb2x lcyI6WyJhc m46YXdzOml hbTo6OTE1M TY0NTUyNjQ 1OnJvbGVcL 0FQSUdhdGV 3YXlFeHRlc m5hbEdyb3V wIiwiYXJuO mF3czppYW0 6OjkxNTE2N DU1MjY0NTp yb2xlXC9BU ElHYXRld2F 5RGV2R3Jvd XAiXSwiYXV kIjoiMnZsM WJobGtjY3I 5dTZkaDM4c XZwbXJ2YzM iLCJldmVud F9pZCI6Ijl iNWVmYmFkL WIxZmYtMTF lOC1hNmM0L WEzY2NmY2J lZTc0OSIsI nRva2VuX3V zZSI6ImlkI iwiYXV0aF9 0aW1lIjoxN TM2MjU3MjA xLCJleHAiO jE1MzYyNjA 4MDEsImlhd CI6MTUzNjI 1NzIwMSwiZ W1haWwiOiJ 0YWxlbmRAb WFzY29jYWJ pbmV0cnkuY 29tIn0.OiR tt_zUlxM6a QAll6rL4zU I10L1ZOJT4 -XXhxjpuf9 TbJUj0En55 yu2QoFXepC 5hBQgoFHCx Lcvgae057e 9H9wL4ylve 4GHwSYTj_2 ue0nblLrEL rtS0b30r3M jIZV5I9L7Z 2H2166FDre YW0QBZYcbJ 6LHpo2GrkW BWGzgZ6p-A IoEL4sz5Ky FMoMAclhG9 uYAmwPX2Jn v62CMgbKtg jdqtdGSa6e Nwlv-GtFoe w4QIIvNdfQ Q2Cegm4nPw 024_xKqBvc genkLkEYzw dNcQEf1lj9 fYnk-szUtp iw6gPaU61j yvFdydGSw0 TKmEUhV40Q K21OiOiS50 NTtlfpD2Q"
}
They are expecting it via Postman, but I am trying to automate it via Powershell.
This is all that I have been given by the API owner:
https://api.xxx.com/xxx/authenticate
Post below request to above URI
{
"username":"oldenkamp",
"password":"<password>"
}
Above request will get you Response as below:
{
"status": "success",
"id_token": "eyJraWQiOiJuZjlzTzhaeENtb
}
They are expecting it via Postman, but I am trying to automate it via Powershell.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Footech, your example got me close enough to produce a result. I did have to make small modifications to fit the API, but it was a good example. Thanks to all for the help.
Open in new window
reference : https://docs.microsoft.com/en-us/powershell/module/mgmtsvcadmin/get-mgmtsvctoken?view=azurepack-ps