0

I'm trying to get a token for the ArcGIS Online service through Javascript, as talked about on their website (under "Using REST") and was wondering if I had the code right.

This is just some Javascript which does execute, but the alert always returns null:

<script type="text/javascript">

    xhr = new XMLHttpRequest();
    var url = "https://www.arcgis.com/sharing/rest/oauth2/token/";
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.onreadystatechange = function ()
    {
        if (xhr.readyState == XMLHttpRequest.DONE)
        {
            alert(xhr.responseXML);
        }
    }
    var MyJSON = JSON.stringify({ "client_id": "<MY_CLIENT_ID>", "client_secret": "<MY_CLIENT_SECRET>" });
    xhr.send(MyJSON);

</script>

I think I've hit the right settings, but does the code look right? It is supposed to return an access_token property.

EDIT - I'm using an alert just to check that it's returning something. Currently it's returning null.

EDIT AGAIN - this is the same code found at this question

AND EDIT AGAIN:

ArcGIS Online seems to be giving the following code samples in Node.js:

var request = require('request'); // npm install request

// generate a token with your client id and client secret
request.post({
url: 'https://www.arcgis.com/sharing/rest/oauth2/token/',
json: true,
form: {
  'f': 'json',
  'client_id': 'YOUR_APPLICATIONS_CLIENT_ID',
  'client_secret': 'YOUR_APPLICATIONS_CLIENT_SECRET',
  'grant_type': 'client_credentials',
  'expiration': '1440'
}
}, function(error, response, body){
console.log(body.access_token);
});

AND

var request = require('request'); // npm install request

request.post({
url: 'http://geoenrich.arcgis.com/arcgis/rest/services/World/GeoenrichmentServer/Geoenrichment/enrich',
json:true,
form: {
  f: 'json',
  token: 'J-S0KLOl5_8UIqzZfmjPp6KQQeN5rnDRxRKB73n7B2hxuuI6Fec09IsIk0n8a0j-LoBskkio0I5fL0sY5iLf1J8lfhgq1gdaOAB15sm2wEaRooZbWz87bWptfGOMlqfFCoGRwF9n0h3tOd21lMyB9g..',
  studyAreas: '[{"geometry":{"x":-117.1956,"y":34.0572}}]'
}
}, function(error, response, body){
console.log(body);
});

But this runs into hurdles with even require('request'); being undefined.

EDIT (again) - installed Node.js and it works, but I can't figure out how to get the access_token out.

Community
  • 1
  • 1
user25730
  • 517
  • 2
  • 6
  • 24
  • try to alert `xhr.responseText`. responseXML is null if the response doesn't contain XML, which seems likely in this scenario (it's probably JSON or just plain text?) -edit- Yes, they doc says it should be JSON. – GolezTrol Jul 22 '16 at 05:20
  • Thanks. Sadly. `xhr.responseText` returns a blank value (not even a null). – user25730 Jul 22 '16 at 05:23
  • I've also tried using `var GetSomeJSON = JSON.parse(xhr.responseText);` but that gets a syntax error. Which does make me think it's returning something now... – user25730 Jul 22 '16 at 05:25
  • Check out [the other properties of XMLHttpRequest](https://developer.mozilla.org/nl/docs/Web/API/XMLHttpRequest#Eigenschappen) too, especially responseType, status and statusText. Maybe you get some insight in why the request failed. – GolezTrol Jul 22 '16 at 05:30
  • The `xhr.status` is 0, which I'm pretty sure is a fail. `xhr.readyState` is 4 which is done. – user25730 Jul 22 '16 at 05:33
  • I'm pretty sure too. Could be a cross origin issue, in which case you may need to request a response in `jsonp` format. I'm not very experience in that area, but my first suggestion would be to use [ArcGIS's own documentation on OAuth](https://developers.arcgis.com/javascript/3/jshelp/ags_secureservices.html) to see how they would like you to connect to them. – GolezTrol Jul 22 '16 at 05:37
  • Just added another edit. – user25730 Jul 22 '16 at 06:02

0 Answers0