Access token and function in standard url not curl

I know you shouldn’t expose your access token in a url, but I’m just trying this on my own server so I can test communicating with my spark through a web browser.

I’m trying to do what curl does: curl https://api.spark.io/v1/devices/xxxxx/led -d access_token=xxxxx -d params=ON (this works in my terminal)
To translate the curl into a GET url is I think so:https://api.spark.io/v1/devices/xxx/led/?access_token=xxxxx&params=ON
but this does not work, although removing the last “&params=ON” results in the access token being recognized by the cloud, but since the params=ON is not there no command is sent to the “led” function. I’ve seen examples of sending these data in JSON format, but shouldn’t I also be able to do this as a straight url. What am I missing here.

Thanks for any help,

Dean

HI @tinoak

The command you are doing in curl does an HTTP POST, not a GET. In curl, any command with a -d is a POST unless you also include -G to curl. Calling a Spark.function() requires a POST request.

I don’t know of a way to POST using just a URL that you type in to a browser. There are some web developer tools like Postman that let your do this, or you can write a small AJAX jquery web page to do this.

@tinoak,

You can use java / jquery and place your token access code in the IDE Code and flash Spark Core. Running Tiny Webserver on the Core as well with the part of TInker IDE Code DigRead / DigWrite / AnalonRead / AnalogWrite. The code below is from Tinker IDE Code. Credit also goes to @BDub for sharing his java code that help me learn how to place Access Token inside Core and not via Browser Command or on Internet in a file.

Let me know if this will work for you and I will link complete code for you.
< CODE EXAMPLE - CORE IDE CODE>

webClient.println(" < script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js”>< /script>< /head>< body>\n\n");

webClient.println("$.post(“https://api.spark.io/v1/devices/48ff6f065067556028091087/digitalwrite”, \n\n");
webClient.println("{ args:“D0,LOW”, access_token: “e2cec1f4d656c3e95091935085f593f0fec135e3” })};< /script>< /p>< br >< br > \n\n");

O.k. then I found this ajax snippet and added it to my code. I’m pretty new to all this web programming, so could you check the code below for any obvious errors? I assume the jQuery library is accessed through the src designation at the top of the script. I added a javascript command to change a “word” on the page to “debug”. The method "doMethod is called and runs through to the javascript which changes the word to “did”. This means the ajax function completes? How can I see the output from the ajax function, to debug?


<html><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">

function on() {   
    document.getElementById("word").innerHTML = "ON";  }
function off() { 
    document.getElementById("word").innerHTML = "OFF";  }
    
       var baseURL = "https://api.spark.io/v1/devices/";

       function doMethod() {
            
            var url = baseURL + "48ff6a065067555048312287" + "/" + "led";
------ here is the ajax part            
            $.ajax({
            type: "POST",
            url: url,
            data: { access_token: "<< token >>", args: "ON" },
            success: function() { $("#result").text("Success"); } ,
              dataType: "json"
            });
            document.getElementById("word").innerHTML = "did";           
        }
</script>
</head>

<body><h2>This is the second page, dude</h2>
<p id="word">empty</p>

<form action="/first.py">
<input type="submit" value="Go to Timer">
</form>

------here is the javascript function call to the ajax function
<input type="submit" value="On" onClick="doMethod();">


<form action="https://api.spark.io/v1/devices/48ff6a065067555048312287/led -d?params=OFF">
<input type="submit" value="Off" onClick="off();">
</form>

</body></html>

Hi @tinoak

I usually use

$.post( requestURL, { params: newValue, access_token: accessToken });

but the more general $.ajax() works great too. I have another tutorial I am working on for Spark.function and POST.

Did this work for you?

You should not post your access token here – that is your personal secret (I fixed your post). You should probably revoke that token and get a new one. There are bunch of ways to do that using the CLI or the build web page. Don’t worry, a bunch of us have done it too! it is easy when cutting and pasting code here to forget.

Hi @tinoak

I put my Spark function POST tutorial up tonight over here:

1 Like

The ajax message works to send a command from web to spark! Thanks. Your tutorial looks perfect to get a lot of us less familiar with web programming up and experimenting.

2 Likes