Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all, Ihave this Linux shell script which gets an Azure Web token
#!/usr/bin/bash
clear
CLIENTID=41eeebbd-ea7f-4f49-a936-1624b6cb9c72
CLIENTSECRET=UIC8Q~nj3cDetR4XQlSNbm19WpywweJMi.CuwbGA
RESOURCE=api://41eeebbd-ea7f-4f49-a936-1624b6cb9c72
TENANTID=90fff3ee-9a7c-4eb1-8259-4d8c6bf6ca90
AZURESITE="https://login.microsoftonline.com/$TENANTID/oauth2/token"
FLAGS="-s -d"
CURL="curl $FLAGS"
COMMANDLINE="$CURL grant_type=client_credentials&client_id=$CLIENTID&client_secret=$CLIENTSECRET&resource=$RESOURCE"
ACCESSTOKEN=$($COMMANDLINE $AZURESITE)
ACCESSTOKEN=$(echo $ACCESSTOKEN|jq .access_token)
ACCESSTOKEN=$(echo $ACCESSTOKEN|sed s/\"//g)
export ACCESSTOKEN
echo $ACCESSTOKEN

I would like to create an equivalent batch file in Windows

What I have tried:

@echo off
setlocal

set CLIENTID=41eeebbd-ea7f-4f49-a936-1624b6cb9c72
set CLIENTSECRET=UIC8Q~nj3cDetR4XQlSNbm19WpywweJMi.CuwbGA
set RESOURCE=api://41eeebbd-ea7f-4f49-a936-1624b6cb9c72
set TENANTID=90fff3ee-9a7c-4eb1-8259-4d8c6bf6ca90
set AZURESITE=https://login.microsoftonline.com/%TENANTID%/oauth2/token
set COMMANDLINE="curl -s grant_type=client_credentials&client_id=%CLIENTID%&client_secret=%CLIENTSECRET%&resource=%RESOURCE%"
echo "Command line is %COMMANDLINE%"

It bombs on the set COMMANDLINE= line with the error 'client_id' is not recognized as an internal or external command - followed by a similar error for client_id etc...
Any idea why guys ?
Posted
Updated 31-Oct-22 22:33pm
v2

The & character in a CMD Prompt is command separator. It says "execute the command on the left of the & character, then execute the command to the right of the & when the left command has completed execution".

To get CMD to process that character as part of the URL, you would have to escape the & characters by preceding them with the ^ character:
@echo off
setlocal

set CLIENTID=41eeebbd-ea7f-4f49-a936-1624b6cb9c72
set CLIENTSECRET=UIC8Q~nj3cDetR4XQlSNbm19WpywweJMi.CuwbGA
set RESOURCE=api://41eeebbd-ea7f-4f49-a936-1624b6cb9c72
set TENANTID=90fff3ee-9a7c-4eb1-8259-4d8c6bf6ca90
set AZURESITE=https://login.microsoftonline.com/%TENANTID%/oauth2/token
set COMMANDLINE="curl -s grant_type=client_credentials^&client_id=%CLIENTID%^&client_secret=%CLIENTSECRET%^&resource=%RESOURCE%"
echo "Command line is %COMMANDLINE%"
 
Share this answer
 
v2
The & in a Windows command is used to allow multiple commands on the same line. So client_id after the first one looks like the next command. You need the entire parameter string in double quotes, so try something like:
BAT
set CMDPARAMS="grant_type=client_credentials&client_id=%CLIENTID%&client_secret=%CLIENTSECRET%&resource=%RESOURCE%"
curl -s "%CMDPARAMS%"
 
Share this answer
 
v2
Thanks to all of you I now have this
@echo off
set CLIENTID=41eeebbd-ea7f-4f49-a936-1624b6cb9c72
set CLIENTSECRET=UIC8Q~nj3cDetR4XQlSNbm19WpywweJMi.CuwbGA
set RESOURCE=api://41eeebbd-ea7f-4f49-a936-1624b6cb9c72
set CLIENTID=41eeebbd-ea7f-4f49-a936-1624b6cb9c72
set CLIENTSECRET=UIC8Q~nj3cDetR4XQlSNbm19WpywweJMi.CuwbGA
set RESOURCE=api://41eeebbd-ea7f-4f49-a936-1624b6cb9c72
set TENANTID=90fff3ee-9a7c-4eb1-8259-4d8c6bf6ca90
set AZURESITE="https://login.microsoftonline.com/%TENANTID%/oauth2/token"
set FLAGS="-s -d"
set CURL="curl %FLAGS%"
set COMMANDLINE="curl -v grant_type=client_credentials^&client_id=%CLIENTID%^&client_secret=%CLIENTSECRET%^&resource=%RESOURCE%"

rem How do I execute the line below ?
rem it expands to this

curl -s grant_type=client_credentials^&client_id=41eeebbd-ea7f-4f49-a936-1624b6cb9c72^&client_secret=UIC8Q~nj3cDetR4XQlSNbm19WpywweJMi.CuwbGA^&resource=api://41eeebbd-ea7f-4f49-a936-1624b6cb9c72 https://login.microsoftonline.com/90fff3ee-9a7c-4eb1-8259-4d8c6bf6ca90/oauth2/token

rem but when executed produces lots of markup but no token
set ACCESSTOKEN=%COMMANDLINE% %AZURESITE% 

set ACCESSTOKEN=echo %ACCESSTOKEN%|jq .access_token
echo %ACCESSTOKEN%
rem set ACCESSTOKEN=echo %ACCESSTOKEN%|C:\MinGW\git\usr\bin\sed.exe s/\"//g
rem echo %ACCESSTOKEN%
 
Share this answer
 
v2
Comments
Richard MacCutchan 1-Nov-22 5:16am    
I don't know curl, but looking at curl - How To Use[^] I think the final command should be something like:
curl -d "grant_type=client_credentials^&client_id=%CLIENTID%^&client_secret=%CLIENTSECRET%^&resource=%RESOURCE%" %AZURESITE%

The -s (silent) and -v (verbose) options may be necessary, but I would suggest only one at a time. It may also be necessary to capture the output to a file, but I leave that to you.
pkfox 1-Nov-22 8:34am    
If I set all the vars from the command line and run this
curl -s -d "grant_type=client_credentials&client_id=%CLIENTID%&client_secret=%CLIENTSECRET%&resource=%RESOURCE%" %AZURESITE%|jq .access_token

it works ( notice I've removed the ^ escape character ) but it still errors out in the batch file.
pkfox 1-Nov-22 5:31am    
The beautiful output



<title>Sign in to your account























//<![CDATA[
$Config={"iErrorDesc":0,"iErrorTitle":0,"strMainMessage":"We received a bad request.","strAdditionalMessage":"","strServiceExceptionMessage":"AADSTS900561: The endpoint only accepts POST, OPTIONS requests. Received a GET request.","strTraceId":"cb3a6f0d-d5fc-41f8-9c9b-94690b911800","iErrorCode":900561,"iHttpErrorCode":400,"iViewId":1,"urlCancel":"","strTimestamp":"2022-11-01T09:14:55Z","urlLearnMoreRiskyApp":"https://go.microsoft.com/fwlink/?linkid=2133457","oResumePostParams":{},"iMaxStackForKnockoutAsyncComponents":10000,"fShowButtons":true,"urlCdn":"https://aadcdn.msauth.net/shared/1.0/","urlDefaultFavicon":"https://aadcdn.msauth.net/shared/1.0/content/images/favicon_a_eupayfgghqiai7k9sol6lg2.ico","urlFooterTOU":"https://www.microsoft.com/en-US/servicesagreement/","urlFooterPrivacy":"https://privacy.microsoft.com/en-US/privacystatement","iPawnIcon":0,"sPOST_Username":"","sFTName":"flowToken","fEnableOneDSClientTelemetry":true,"urlReportPageLoad":"https://login.microsoftonline.com/common/instrumentation/reportpageload?mkt=en-US","dynamicTenantBranding":null,"staticTenantBranding":null,"oAppCobranding":{},"iBackgroundImage":2,"fApplicationInsightsEnabled":false,"iApplicationInsightsEnabledPercentage":0,"urlSetDebugMode":"https://login.microsoftonline.com/common/debugmode","fEnableCssAnimation":true,"fAllowGrayOutLightBox":true,"fIsRemoteNGCSupported":true,"fUseSameSite":true,"uiflavor":1001,"fOfflineAccountVisible":false,"fEnableUserStateFix":true,"fShowAccessPassPeek":true,"fUpdateSessionPollingLogic":true,"fUseNewAuthenticatorIcon":true,"fDetectMultiHighContrastThemes":true,"fFetchSessionsSkipDsso":true,"scid":1013,"hpgact":1801,"hpgid":1117,"pgid":"ConvergedError","apiCanary":"AQABAAAAAAD--DLA3VO7QrddgJg7Wevrs7KbdxKpBUOZhqjuYy6kGKqs7mpv6-VNSTes4n1KrZgZefUVcE62EYPgzicVelouJU6Ul_KwO-j-PPs30JzkTtlEWLSq8SpLZZfRwGinOOqSm7thwpfhj3Rq1qLyxXZ_f3j5SpqIyefj_Xe15pilotFhyvPxuN7ogqXUcyPhQ4LlwHef7OdL7hsfPGaWjzfrdFGkAdZlyr7uYpybAzK8BiAA","canary":"RZupQpVcS6yQ8XeRmjAKH9MSZlIQ1SsAUv7ZA7fizEo=7:1","correlationId":"13d5c048-43b5-4513-8361-33d075635bb9","sessionId":"cb3a6f0d-d5fc-41f8-9c9b-94690b911800","locale":{"mkt":"en-US","lcid":1033},"slMaxRetry":2,"slReportFailure":true,"strings":{"desktopsso":{"authenticatingmessage":"Trying to sign you in"}},"enums":{"ClientMetricsModes":{"None":0,"SubmitOnPost":1,"SubmitOnRedirect":2,"InstrumentPlt":4}},"urls":{"instr":{"pageload":"https://login.microsoftonline.com/common/instrumentation/reportpageload","dssostatus":"https://login.microsoftonline.com/common/instrumentation/dssostatus"}},"browser":{"ltr":1,"_Other":1,"Full":1,"RE_Other":1,"b":{"name":"Other","major":-1,"minor":-1},"os":{"name":"Unknown","version":""},"V":-1},"watson":{"url":"/common/handlers/watson","bundle":"https://aadcdn.msauth.net/ests/2.1/content/cdnbundles/watson.min_q5ptmu8aniymd4ftuqdkda2.js","sbundle":"https://aadcdn.msauth.net/ests/2.1/content/cdnbundles/watsonsupportwithjquery.3.5.min_dc940oomzau4rsu8qesnvg2.js","fbundle":"https://aadcdn.msauth.net/ests/2.1/content/cdnbundles/frameworksupport.min_oadrnc13magb009k4d20lg2.js","resetErrorPeriod":5,"maxCorsErrors":-1,"maxInjectErrors":5,"maxErrors":10,"maxTotalErrors":3,"expSrcs":["https://login.microsoftonline.com","https://aadcdn.msauth.net/","https://aadcdn.msftauth.net/",".login.microsoftonline.com"],"envErrorRedirect":true,"envErrorUrl":"/common/handlers/enverror"},"loader":{"cdnRoots":["https://aadcdn.msauth.net/","https://aadcdn.msftauth.net/"],"logByThrowing":true},"serverDetails":{"slc":"ProdSlices","dc":"NEULR1","ri":"DB5XXXX","ver":{"v":[2,1,14006,10]},"rt":"2022-11-01T09:14:55","et":0},"clientEvents":{"enabled":true,"telemetryEnabled":true,"useOneDSEventApi":true,"flush":60000,"autoPost":true,"autoPostDelay":1000,"minEvents":1,"maxEvents":1,"pltDelay":500,"
Richard MacCutchan 1-Nov-22 9:23am    
Pete, can you add all the details to your question please? The formatting in the comments section is a mess still.
pkfox 1-Nov-22 11:31am    
Hi Richard , I agree it is a mess but I don't know how to add things to the original question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900