Generating OAuth Access Tokens through Ajax

EDIT: Solved

Needed to set basic authorization:

    beforeSend: function(xhr) {
        xhr.setRequestHeader("Authorization", "Basic" + bota("particle:particle"));

I’m trying to generate an access token via Ajax. When I use curl from a terminal, it works fine:

curl -u partice:particle -d grant_type=password -d -d password=testpassword -d expires_in=0

When I try using an Ajax call to do it, like so:

    type: "POST",
    url: "",
    data: {
      username: "",
      password: "testpassword",
      expires_in: 0,
      grant_type: "password"
    complete: function(jqXHR, textStatus) {
      console.log("jqXHR", jqXHR);
      console.log("textStatus", textStatus);

I get an error 400:

{ "error": "invalid_client", "error_description": "Invalid or missing client_id parameter" }

Am I missing a header in AJAX that curl puts in? I’ve tried doing a verbose curl and it doesn’t appear that any client_id is set. Do I simply need to have an organization with an OAuth client ID included? If so, why doesn’t curl need one?

1 Like