3

Hi this is my first post so if I haven't formatted something correctly or am not on topic please let me know.

I need to use cURL to access a database and extract datapoints for each device stored in the cloud. My question thus far is how do I save the access token from this line:

`curl -X POST -d "<user><email>[email protected]</email><password>Passwordexample</password><application><app_id>anidexample_id</app_id><app_secret>asecretexample_secret</app_secret></application></user>" -H "Content-Type:application/xml" https://user.aylanetworks.com/users/sign_in.xml`

Into a variable.

So far I've used :

 @echo off
    set /p UserInput= Enter the Access Token:
    @echo on

    curl -H "Authorization: auth_token %UserInput%" https://ads-dev.aylanetworks.com/apiv1/dsns/AC000W000007000/properties.xml>TestFile.xml

This passes the token but it needs to be entered manually every time. I've tried to use set without needing user input and I haven't been able to get it working. Is there something that I'm missing with setting variables?

Any help would be greatly appreciated,

DM

2
  • Between which <TAG></TAG> is the token ? Commented Jun 19, 2014 at 18:49
  • The command output looks like this:<?xml version="1.0" encoding="UTF-8"?> <authorization> <access-token>0317080d361a430bb81e3997114267bf</access-token> <refresh-token>c696753bddb4459c9a8ceb54fa04d53b</refresh-token> <expires-in type="integer">86400</expires-in> <role>OEM::Staff</role> <role-tags type="array"/> </authorization> Commented Jun 19, 2014 at 18:50

1 Answer 1

4

Assuming that the CURL's XML response look like this :

<?xml version="1.0" encoding="UTF-8"?> 
 <authorization> 
   <access-token>0317080d361a430bb81e3997114267bf</access-token>
   <refresh-token>c696753bddb4459c9a8ceb54fa04d53b</refresh-token> 
   <expires-in type="integer">86400</expires-in> 
   <role>OEM::Staff</role> 
   <role-tags type="array"/> 
    </authorization> 

You can try :

@echo off

set URL="<user><email>[email protected]</email><password>Passwordexample</password><application><app_id>anidexample_id</app_id><app_secret>asecretexample_secret</app_secret></application></user>"

for /f "skip=2 tokens=3 delims=^<^>" %%a in ('curl -X POST -d  %URL% -H "Content-Type:application/xml" https://user.aylanetworks.com/users/sign_in.xml') do (
  set "$token=%%a"
  goto:next)

:next
echo The token is : %$token%
pause
curl -H "Authorization: auth_token %$token%" https://ads-dev.aylanetworks.com/apiv1/dsns/AC000W000007000/properties.xml>TestFile.xml

I putted a PAUSE before sending the second request that you can check if there is a correct value in %$token%

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you that answer worked for me! I used something similar before and I am wondering why you wrote %%a instead of %a?
You use %a only when you run a command directly in the CMD in a bat file you have to double the %.
Oh I forgot about that rule! You're right that makes a huge difference. Thank you again I really appreciate the answer.

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.