Can someone walk me through getting a token or token wrapped in a cookie from an ESRI Portal and then where and how to pass it to the REST API to pull the geojson from a feature layer service?
Here is an example on how to create a token using Python:
<SPAN class="keyword token">import</SPAN> requests<SPAN class="punctuation token">,</SPAN> json <SPAN class="comment token"># Disable warnings</SPAN> requests<SPAN class="punctuation token">.</SPAN>packages<SPAN class="punctuation token">.</SPAN>urllib3<SPAN class="punctuation token">.</SPAN>disable_warnings<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> portal <SPAN class="operator token">=</SPAN> <SPAN class="string token">'portal.esri.com'</SPAN> username <SPAN class="operator token">=</SPAN> <SPAN class="string token">"jskinner@ESRI"</SPAN> password <SPAN class="operator token">=</SPAN> <SPAN class="string token">"**********"</SPAN> tokenURL <SPAN class="operator token">=</SPAN> <SPAN class="string token">'https://{0}:7443/arcgis/sharing/rest/generateToken/'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>portal<SPAN class="punctuation token">)</SPAN> params <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'f'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'pjson'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username'</SPAN><SPAN class="punctuation token">:</SPAN> username<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN><SPAN class="punctuation token">:</SPAN> password<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'client'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'requestip'</SPAN><SPAN class="punctuation token">}</SPAN> r <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>post<SPAN class="punctuation token">(</SPAN>tokenURL<SPAN class="punctuation token">,</SPAN> data <SPAN class="operator token">=</SPAN> params<SPAN class="punctuation token">,</SPAN> verify<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN> response <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">)</SPAN> token <SPAN class="operator token">=</SPAN> response<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>token<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Jake already gave a good snippet to generate a token from python.
For the next step you need, to send a request to rest-endpoint to retrieve the GeoJSON from a feature layer, this could be done via a query operation with f and token parameters,e.g:
https://machine.domain.com/webadaptor/rest/services/USAStatesRiversCapitals/FeatureServer/2/query?where=1=1&f=geojson&token=tokenstring
so making an HTTP request to above URL with whatever programing language you're using.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.