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.