I'm trying to access admin APIs for my feature service, but I'm unable to get a valid token. I saw this post and followed along (https://community.esri.com/t5/arcgis-rest-api-questions/generating-token-for-arcgis-online-hosted-se...) and I was able to generate a token. However, when I add that token into my URL to access the admin services, it says Invalid Token. Is there another way I should be generating this token to get the access I need?
URL to generate token looks like this:
https://www.arcgis.com/sharing/generateToken?f=json&username=USERNAME&password=PASSWORD&client=requestip
URL to try to access the feature service admin APIs looks like this:
https://services6.arcgis.com/qDx.../arcgis/rest/admin/services/Layer_Name/FeatureServer?token=2nt...
Error:
Hi Matthew,
There are lots of ways to generate a token for ArcGIS Online. My preferred way is using the application credentials for an item in my ArcGIS Online organization. The steps are:
Send this as a GET or POST request, and a token should be returned that can be used for all content owned by/shared with you to in your ArcGIS Online organization.
I hope this helps!
-Calvin
I do this with RestSharp. Instead of appending the token onto the end of your request URL, you add the token to the request using the AddHeader method.
var client = new RestClient(baseUrl); //the url of your service
var request = new RestRequest("0/applyEdits", Method.POST);
request.AddHeader("Authorization", $"Bearer {token}"); //"token" is a parameter passed into the method; I left the method signature off this sample
request.AddParameter("f", "json");
request.AddParameter(editsType.ToString(), jsonAdds); //"jsonAdds" is another parameter of type JSON object
if (editsType == applyEditsType.adds) //"editsType" is a custom enum parameter
{//allows service to create new GlobalID for adds
request.AddParameter("useGlobalIds", "false");
}
else
{//allows updates and deletes to look for records based on GlobalID instead of OBJECTID
request.AddParameter("useGlobalIds", "true");
}
var response = client.Execute(request);
if (response.ErrorException != null)
{
var gisException = new Exception("Error retrieving response. Check inner details for more info.", response.ErrorException);
throw gisException;
}
return response.Content;