Error Code 400: access token not found

Hey everyone,
I just got my spark core last week and have been trying to communicate with it with my computer. I am trying to get the “CONTROL LEDS OVER THE ‘NET” code working with the on board led in digital pin 7, and I’ve modified the code accordingly. I’m using the terminal app on a mac. This is what I send:

curl https://api.spark.io/v1/devices/[MY_DEVICE_ID]/led\
-d access_token=[ACCESS_TOKEN]
-d params=l1,HIGH

And what I get:

“code”: 400,
“error”: “invalid_request”,
“error_description”: “The access token was not found”
}curl: (6) Could not resolve host: access_token=[ACCESS_TOKEN]-d
curl: (6) Could not resolve host: params=l1,HIGH

I then tried using the spark helper at github but the led does not turn on. Can someone please help me? I just want to learn to turn on and off the led.
Just in case this is the code I’m using (I copied the entire thing from the annotated examples):

int led1 = D7;

void setup()
{
Spark.function(“led”, ledControl);
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
}

void loop()
{

}

int ledControl(String command)
{
int state = 0;
int pinNumber = command.charAt(1) - ‘0’;
if (pinNumber < 0 || pinNumber > 1) return -1;
if(command.substring(3,7) == “HIGH”) state = 1;
else if(command.substring(3,6) == “LOW”) state = 0;
else return -1;
digitalWrite(pinNumber, state);
return 1;
}

Thanks
ED

1 Like

Hi Ed,
Just to check, the 2 tags wrapped wrapped in the square brackets, ([ACCESS_TOKEN] & [MY_DEVICE_ID]), need to replaced with your values.

For example, when you browse to: https://www.spark.io/build/
Click on the bottom left icon which is Settings, and in here you will find your access token.
Click on the icon above that, which is Cores, and click on the > arrow next to your core. This will give you its device id.

Replace the tags with these values and without the square brackets.

2 Likes

Thanks for responding @asher, but that is not the problem. I know what values to put there, I just replaced them in my post.

Hey @mena8,

Just popping in to see if you got this resolved or no. I think the error message from curl is saying it got confused about the parameters, probably because of how the line was split?

Maybe try something without the ending ""s:

curl https://api.spark.io/v1/devices/[MY_DEVICE_ID]/led -d access_token=[ACCESS_TOKEN] -d params=l1,HIGH

Thanks!
David

Im having the same problem here. Im using this to turn ON/OFF the lights : http://jflasher.github.io/spark-helper/. But with the terminal nothing work.

I have this all the time:

{
“code”: 400,
“error”: “invalid_request”,
“error_description”: “The access token was not found”
}

Hi @cypriano,

Make sure you’re doing a “POST” request on the function side, and paste your access code at the top. If that’s still not working, feel free to PM me your core id, and I can see what’s going on in the logs.

Thanks,
David

Hi @Dave,

Whts means POST request on the function side?

Now I managed to make it work, im not good in programming (i work with design) but i think that my problem was in the copy and paste (noob), i was copying the function to a note, and in this text i paste my device_ID and access_token, and after copy all this and paste on terminal. In this way was not working because the text format was going bad. After i change my text editor and work good.

Now if i use the terminal work, but if i try chrome or safari don’t work.

Terminal:
curl https://api.spark.io/v1/devices/111111111111111/led
-d access_token=11111111111
-d params=l1,HIGH

i got:

{
“id”: “111111111111”,
“name”: “xxxxxxx”,
“last_app”: null,
“connected”: true,
“return_value”: 1
}

but if i try this on chrome or safari:

https://api.spark.io/v1/devices/111111111111111/led
-d access_token=11111111111
-d params=l1,HIGH

i got:
{
“code”: 400,
“error”: “invalid_request”,
“error_description”: “The access token was not found”
}

wht im doing wrong?

Tnks !

Lucas

Hi @cypriano,

Good question! When you put a url into a browser and hit enter, most of the time your browser is making a “GET” request. As in, “Please GET me this web page, thanks!”

When you want to effect a change on a web service, most of the time you want to do a http “POST” request like when you submit a form, or call an api endpoint.

Wikipedia has a good article on this here: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

Thanks!
David

Tnks again @Dave

One last thing, do you now some way that i can use this POST request from my iphone? Or the way that i should use this code in a xcode project for a app?

The code that i use Terminal:

curl https://api.spark.io/v1/devices/xxxxxxxxxxxxx/led
-d access_token=xxxxxxxxx
-d params=l1,HIGH

Now, what i can use to send this with a phone or how i can write this in a app for a phone?

In any case, thks!!

Hi @cypriano - you can definitely do this from an iPhone. I am not familiar with Objective C myself but if you search for tools for interfacing with REST APIs, I am sure you will find a number of great tools. A quick Google search turned up this guy:

http://restkit.org/

1 Like

Checkout jflasher’s app and bdub’s app here:

and

Thanks!
David

1 Like

@cypriano, I posted an Xcode project here ,http://jmp.sh/v/78jvj55esivQl2rAmFRb that gives examples of GET and POST requests in Objective-C.

3 Likes

This is a good reminder, I think we probably need a list of examples and libraries that make it easy to use the API in all the different languages that have been posted. There is also a nice python wrapper, and a few node wrappers, not to mention Johnny-Five and others!

Nice,

@Ric can you post the code for the core that you use in this project?

@Dave i used the https://github.com/technobly/Simple-Spark-Core-Controller and was really easy!

I think that this its really important, im really have a nice experience in my firsts days with the spark core, and sounds that this is a great project with potential for people without programer background. One concrete thing, if you can make the “CONTROL LEDS OVER THE ‘NET” tutorial with different languages will be really useful.

Tnks for all!

2 Likes

@cypriano, that code is included in the project I linked to as a comment in the ViewController.m file.