How to generate a valid token for ArcGIS Online Feature Service

2932
2
05-26-2021 10:09 AM
MatthewFogarty
New Contributor II

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:

Screen Shot 2021-05-26 at 10.08.24 AM.png

2 Replies
by Anonymous User
Not applicable

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:

  1. Browse to the Content tab of your ArcGIS Online organization. Add Item -> An Application of the type "Application"
  2. Go to the Item Details page of the created item and browse to the Settings tab. At the bottom of the page, expand "Registered Info". You should see an AppID and AppSecret.
  3. Go to the Oauth2.0 token URL: https://www.arcgis.com/sharing/rest/oauth2/token?. The URL parameters we need are:
  • f = json
  • client_id = your AppID from above
  • client_secret = your AppSecret from above
  • grant_type = client_credentials
  • expiration = 20160 (2 weeks in minutes, this is the maximum)

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

 

BethLott
New Contributor II

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;
0 Kudos