1

I need to get a token which I'm getting from one API call to use it in another API call.

I'm trying to extract the access_token part somehow, to define it as a variable which I use in the 2'nd call.

My command:

Invoke-RestMethod -Uri $URI_TOKEN -Method Post -Headers $HEADERS_TOKEN -Body $BODY_TOKEN | Select-String -pattern access_token

Output:

@{access_token=eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI4dDdPZUhRa1prall4SW1NWko3YVROanFMeW11WngwNWhvWTBxekxBbk1jIn0.eyJleHAiOjE2MTI4Mjg0OTMsImlhdCI6MTYxMjgyODQzMywianRpIjoiZGZkNTY5ZGUtYWUyZ
i00NDRhLTk0NTgtNzY2NWYwN2UzZjkyIiwiaXNzIjoiaHR0cHM6Ly92ZXJpZmljYXRpb24taWFtLXdydS50ZWxpdGNhYXNtZ210LnQtaW50ZXJuYWwuY29tL2F1dGgvcmVhbG1zL21hc3RlciIsInN1YiI6IjMwZjIyZDFhLTYxZTQtNGM5YS05OGU1LTU0MjhmZWM1M
zViMiIsInR5cCI6IkJlYXJlciIsImF6cCI6ImFkbWluLWNsaSIsInNlc3Npb25fc3RhdGUiOiI4NjRlODYxNi00NTJlLTRiNGItYjBkMC1kMjIxYjEwNDA5OWUiLCJhY3IiOiIxIiwic2NvcGUiOiJwcm9maWxlIGVtYWlsIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlL
CJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhZG1pbiJ9.Kz4zsCKyFFrvxm4ZLaf-pE2iDQOFF_Y_HPSTWslVBKqu9QnMaPVQINWENvcydP5SKg4ABza3OGkLreBhPmfeDGupRVIQNU5h4C5AewbVcNke1Drvi2CE3nEopqigIYE1Ab_GwovXT8MuXt1NTbpPtLgnjVuj-Q4P
pHKvdtHomf_DY1PTR9aFQ6CLohHmLEjBiOtOcnce0r6ZI_--XFDL81UkOOEW062LMYqyUV-jb8uRjlbUuKWHdUKnlr2H_bQNFQ5vVvi0BgevDrBc6E4oECiMnpIVFitXVIF6ZtPhKT-lyGkyZ7oaXERT-v_AcQLy2-t-cscsd0bGCL2zbUOELw; expires_in=60; r
efresh_expires_in=1800; refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJlNTkxZWEyZC1jMDM1LTQ4YWUtYWY0Yi0wYjc1ZGY4YzA5ZmIifQ.eyJleHAiOjE2MTI4MzAyMzMsImlhdCI6MTYxMjgyODQzMywianRpIjoiMTA
wNjZhYWYtMGIzNC00MTBjLWI2MTItNmFmNDI5NjAwNDQ2IiwiaXNzIjoiaHR0cHM6Ly92ZXJpZmljYXRpb24taWFtLXdydS50ZWxpdGNhYXNtZ210LnQtaW50ZXJuYWwuY29tL2F1dGgvcmVhbG1zL21hc3RlciIsImF1ZCI6Imh0dHBzOi8vdmVyaWZpY2F0aW9uLWl
hbS13cnUudGVsaXRjYWFzbWdtdC50LWludGVybmFsLmNvbS9hdXRoL3JlYWxtcy9tYXN0ZXIiLCJzdWIiOiIzMGYyMmQxYS02MWU0LTRjOWEtOThlNS01NDI4ZmVjNTM1YjIiLCJ0eXAiOiJSZWZyZXNoIiwiYXpwIjoiYWRtaW4tY2xpIiwic2Vzc2lvbl9zdGF0ZSI
6Ijg2NGU4NjE2LTQ1MmUtNGI0Yi1iMGQwLWQyMjFiMTA0MDk5ZSIsInNjb3BlIjoicHJvZmlsZSBlbWFpbCJ9.fwY1IUnWkS-pMzy_-pBiqch4dL4eLMvCPSKaAsGeqqw; token_type=bearer; not-before-policy=0; session_state=864e8616-452e-4
b4b-b0d0-d221b104099e; scope=profile email} 

This output contains items I don't need. Is it possible to extract the string which starts with access_token=blahblah and until the ; sign only (up until expires_in=60 basically)?

Many thanks in advance!

7
  • 1
    Capture your result in a variable, then replace everything except the token with something like $Results -replace '^.*access_token=(.*?);.*','$1' Commented Feb 9, 2021 at 0:11
  • 1
    You'll probably find it easier to extract the access token if you don't pipe the output from Invoke-RestMethod to Select-String. Commented Feb 9, 2021 at 0:42
  • @TheMadTechnician thank you for your answer, but because of many broken lines (spaces) in that output that didn't really workout in a way that it would be consumable by the next step of the script, where I need to provide the bearer token for authentication. Commented Feb 9, 2021 at 9:04
  • @MathiasR.Jessen, maybe that is the case indeed, but then output is very long and not suitable for substitution, I can't paste it, too many characters. I'm not sure what would be the best practice in this case, I will google how people build such API calls scripts, hopefully i will find something. I need to get token in the first step, then in the 2'nd step add users to the system using that bearing token for authorization. Commented Feb 9, 2021 at 9:05
  • 1
    What does (Invoke-RestMethod -Uri $URI_TOKEN -Method Post -Headers $HEADERS_TOKEN -Body $BODY_TOKEN).GetType() reveal? Is is a string, or a PSObject ? Commented Feb 9, 2021 at 10:31

1 Answer 1

1

Since you have shown that the output of the Invoke-RestMethod is a PSCustomObject, you can simply reference the access_token property by doing this:

$token = (Invoke-RestMethod -Uri $URI_TOKEN -Method Post -Headers $HEADERS_TOKEN -Body $BODY_TOKEN).access_token
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.