0

I have data that is hosted by ArcGIS Online.

I want to query ArcGIS Online via an HTTP request. The response would be a JSON object (similar to this).


What would it take to query a hosted ArcGIS Online FC via an HTTP request?

2 Answers 2

3

Based on the previous answers, I have come across the following implementation:

Step 1 - Register an application on ArcGIS online.

  1. On you AGOL content page click Add Item > An Application.
  2. On the Add an application window, select Application and give appropriate Title and Tags.
  3. You will be redirected to the application overview page. Click Setting at the top right corner.
  4. All the way at the bottom click Registered Info.
  5. The information map as follows: a. App ID = client_id b. App Secret = client_secret

Step 2 - Generate the token.

var client_id = "Your App Id";
var client_secret = "Your App Secret";
var grant_type = "client_credentials";
var feature_layer = 'https://sampleserver6.arcgisonline.com/arcgis/rest/services/LocalGovernment/Events/MapServer/0' // It will be your hosted feature layer
var urlTokenGenerator = `https://www.arcgis.com/sharing/oauth2/token?client_id=${client_id}&client_secret=${client_secret}&grant_type=${grant_type}&f=json`

// Generate a token
fetch(urlTokenGenerator).then(function (res) {
  return res.json();
}).then(function (data) {
  let _token = data["access_token"]
  console.log(`Your token is::${_token}`)
}

Step 3 - Use the generated token on the subsequent request.

Code example:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
<code id="code"></code>

  <script>
    var client_id = "Your App Id";
    var client_secret = "Your App Secret";
    var grant_type = "client_credentials";
    // It will be your hosted feature layer
    var feature_layer = 'https://sampleserver6.arcgisonline.com/arcgis/rest/services/LocalGovernment/Events/MapServer/0' 
    var urlTokenGenerator = `https://www.arcgis.com/sharing/oauth2/token?client_id=${client_id}&client_secret=${client_secret}&grant_type=${grant_type}&f=json`
    
    // Generate a token
    fetch(urlTokenGenerator).then(function (res) {
      return res.json();
    }).then(function (data) {
      let _token = data["access_token"]
      console.log(`Your token is::${_token}`)
      // Attaching the token to request url
      var fl_url = `${feature_layer}/query?returnGeometry=false&where=1%3D1&outFields=*&f=pjson&token=${_token}`
      fetch(fl_url).then(function (res) {
        return res.json()
      }).then(function (data) {
        let str = JSON.stringify(data, null, 2);
        document.getElementById("code").innerText = str;
        console.log(data); // *data* is your response from the sever
      })
    })
  </script>
</body>

</html>
3

You will first need to make a request to AGOL to get an OAuth token, then append that token to your query request ("&token=<token)".

https://www.arcgis.com/sharing/oauth2/token?client_id=<id>&client_secret=<secret>&grant_type=client_credentials

You will need to registed an app in AGOL to get a clientID/Secret for your account, then plug those in above to get a token.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.