GET format for access_tokens

I try to get the token with HTML file. But don’t know the format. I tested with curl according to doc as follow and it works.

curl https://api.spark.io/v1/access_tokens -u joe@example.com:SuperSecret

But when testing with $.getJSON(requestURL, function(data){}); in HTML, it does not work.
What is the format should be ?

var requestURL = "https://api.spark.io/v1/access_tokens -u joe@example.com:SuperSecret"

Heya @Dilbert!

jQuery requires a slightly different way to pass in a HTTP Basic Auth token:

$.ajax({
  url: 'https://api.spark.io/v1/access_tokens',
  beforeSend: function (xhr) {
    xhr.setRequestHeader("Authorization", "Basic " + btoa('username:password'));
  },
  success: function(data) {
    // here you go!  example:
    console.log(data);
    alert('first token: '+data[0].token);
  }
});

You might want to take a look at sparkjs though, as it abstracts all that for you!

With sparkjs:

spark.login({username: 'foo@bar.com', password: 'pw'}, function(err, body) {
  console.log(body.access_token);
});

Enjoy! :sunflower:

Thanks, it works. Is the sparkjs recently available. I did not notice this before.

Also, is this normal my core would have 6 Access Token assigned. One is User and other 5 are Spark related.

Very fresh indeed - started 9 day ago :smile:

First at all, they are assigned to your account, not to a single core.
The number of access tokens depend on your different use cases - you’ll see only one for the spark-ide but most likely many for spark-cli, spark, Spark and so on. Any time you login again via one of the many ways, a new token is created. To avoid -> reuse your tokens :sunny: To cleanup -> use the API :hammer:

Getting the access token with user email+password and it works.

But when I try different command, get list of devices in account. Then it does not work. Any ideas ?

url: “https://api.particle.io/v1/devices