[SOLVED]Access Tokens not working with API

Its been 3 days since I am struggling to get LED on off via Web app Example work through API. It appeared to me that Access token is no more valid which is shown in Build. I created more tokens also.
I also tried generating new tokens and using them all. None worked. Now I have deleted the redundant ones. And still only Spark Dev Works on my Mac to write the cloud functions. Nothing else does. If I enter CURL from command prompt or from Web Page, I get error 403.

Currently there are 3 tokens issues to me. And none of them is working, I need guidance how to encode them or access through my web app and command line.

Here is log.

hassanraza$ curl https://api.spark.io/v1/access_tokens -u username:password
[
  {
    "token": "08dxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx5b",
    "expires_at": null,
    "client": "user"
  },
  {
    "token": "33exXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxdb",
    "expires_at": "2015-06-04T09:49:33.298Z",
    "client": "spark-ide"
  },
  {
    "token": "1a6xXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx96",
    "expires_at": "2015-07-22T07:47:38.724Z",
    "client": "__PASSWORD_ONLY__"
  }
]

hassanraza$ curl https://api.spark.io/v1/access_tokens/08dxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx5b
{
  "ok": false,
  "error": "Not Found",
  "device_name": "gerbil lawyer monkey scrapple power"
}
hassanraza$ curl https://api.spark.io/v1/access_tokens/33exXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxdb
{
  "ok": false,
  "error": "Not Found",
  "device_name": "crazy scrapple scraper power power"
}
hassanraza$curl https://api.spark.io/v1/access_tokens/1a6xXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx96
{
  "ok": false,
  "error": "Not Found",
  "device_name": "robot station cowboy trochee laser"

@hassanphd,

the proper way to use the access token is:

# You type in your terminal
curl https://api.spark.io/v1/devices  \
     -d access_token=xxxxxxxxxxxxxx
# Response status is 200 OK, which means
# the Core says, "Yes ma'am!"

To use it in a browser url format:

https://api.spark.io/v1/devices?access_token=ACCESSTOKEN_HERE

More information about API can be found here: http://docs.spark.io/api/

1 Like

I am sorry in advance for posting noob level questions. Thanks for prompt response here. Dear @kennethlimcp I am still getting same error. Actually I am trying to run the jQuery AJAX with Device ID and Access Token stored in HTML file. It appears that:

Entering https://api.spark.io/v1/devices?access_token=08dxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx5b
in browser shows the expected result:

[
  {
    "id": "53ffXxXxXxXxXxXxXxXxXxX7",
    "name": "hasanphd",
    "last_app": null,
    "last_heard": "2015-04-23T10:32:14.931Z",
    "connected": true
  }
]

But when I press the key on my web app it returns this error in console:

{
  "error": "Permission Denied",
  "info": "I didn't recognize that core name or ID, try opening https://api.spark.io/v1/devices?access_token=08dxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx5b "
}

Here is HTML code:


var accessToken = "08dxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx5b";
var deviceID = "53ff6cXxXxXxXxXxXxXxXxXx5"
var url = "https://api.spark.io/v1/devices/" + deviceID + "/led";

function switchOn()
{
	$.post(url, {params: "on", access_token: accessToken });
}  

function switchOff()
{
	$.post(url, {params: "off", access_token: accessToken });
}  

<body>

On/Off Control</h1>	
	
input type="button" onClick="switchOn()" value="ON"/>
input type="button" onClick="switchOff()" value="OFF"/>

The error message is saying it doesn’t recognize your Core ID, whereas the accesstoken appears to be correct.
In your API browser response, the CoreID ends on a 7 and in your web app on a 5. I don’t know if your using different cores, but that might be a cause of problems. Try issuing the browser API command, and then copy&paste the CoreID it gives you into your webapp.

2 Likes

Thanks @Moors7 for your kind help. I think the solution was in manually typing the Spark Core ID in HTML file. It just got working. Matter resolved. Thank you Spark Community.

2 Likes