Geographic Information Systems Asked on May 12, 2021
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?
Based on the previous answers, I have come across the following implementation:
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}`)
}
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>
Correct answer by GeoDev on May 12, 2021
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.
Answered by ericoneal on May 12, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP