Diese Frage gehört wahrscheinlich nicht nur zu einer einzigen Entwicklergruppe, aber die Esri-Leaflet-Implementierung ist das ultimative Endziel und hat wahrscheinlich Entwickler, die mit dem Stack am besten vertraut sind – also hier geht's los:<\/P>
<\/P>
Ich habe eine eigenständige ArcGIS-Server-Situation und möchte auf sichere Feature-Services zugreifen. Meine Seite läuft auf PHP 7.3 und hat Zugriff auf cURL. Idealerweise möchte ich das Token mit Anmeldedaten, die in PHP gespeichert sind, über cURL abrufen und dieses Token dann an Esri-Leaflet übergeben, wenn ich addtomap ausführe, um die Anmeldedaten sicher zu halten. <\/P>
Der Fehler, dem ich bei der Antwort auf die Anfrage begegne, ist der eher generische:<\/P>
{"error":{"code":498,"message":"Invalid Token","details":[]}}<\/P><\/BLOCKQUOTE> <\/P>Vorübergehend gibt es hier eine URL:<\/P>http:\/\/52.65.144.150\/esri-js-demo-page\/<\/A><\/P> <\/P>Was habe ich bisher gemacht? Lassen Sie uns einen gesicherten esri-Demo-Service zur Veranschaulichung verwenden, wobei ich kommentierte Abschnitte belassen habe, in denen ich entweder:<\/P>Versucht habe, den Referer manuell zu setzen, damit cURL denselben Referer verwendet wie der vom Site ausgegebene Referer (hoffentlich). Dies beinhaltete die Seite, die die Karte aufruft, wie man durch Betrachten des im Chrome übergebenen Requests-Referers sieht, und auch durch Sicherstellung, dass der von PHP ausgegebene Referer mit dem von Chrome gesendeten übereinstimmt. Insgesamt möchte ich diesen Ansatz nicht verwenden, da ich möchte, dass mehrere Seiten dieses PHP-Skript nutzen.<\/LI>Versucht habe, stattdessen die IP-Adressen der Besucher zu verwenden. Dies erzeugte ein Token, führte aber zum gleichen Invalid Token-Problem. Meine Organisation kann Anfragen auch von verschiedenen Endpunkten aus senden, was ein möglicher Instabilitätspunkt ist.<\/LI>Versucht habe, den Referer mit anderen PHP-Methoden wie $_SERVER['HTTP_REFERER']; zu setzen.<\/LI>Sie können auch meinen Regex zum Parsen der Tokens sehen – dieser entfernt den Rest der Antwort, so dass nur noch der Token-String übrig bleibt. {".,}. Ich übergebe somit während der Layer-Anfrage kein Ablaufdatum zurück an den Server – ich sehe in keiner Dokumentation, dass dies erforderlich ist.<\/LI><\/UL> <\/P> <\/P> <?php
{"error":{"code":498,"message":"Invalid Token","details":[]}}<\/P><\/BLOCKQUOTE>
Vorübergehend gibt es hier eine URL:<\/P>
http:\/\/52.65.144.150\/esri-js-demo-page\/<\/A><\/P> <\/P>Was habe ich bisher gemacht? Lassen Sie uns einen gesicherten esri-Demo-Service zur Veranschaulichung verwenden, wobei ich kommentierte Abschnitte belassen habe, in denen ich entweder:<\/P>Versucht habe, den Referer manuell zu setzen, damit cURL denselben Referer verwendet wie der vom Site ausgegebene Referer (hoffentlich). Dies beinhaltete die Seite, die die Karte aufruft, wie man durch Betrachten des im Chrome übergebenen Requests-Referers sieht, und auch durch Sicherstellung, dass der von PHP ausgegebene Referer mit dem von Chrome gesendeten übereinstimmt. Insgesamt möchte ich diesen Ansatz nicht verwenden, da ich möchte, dass mehrere Seiten dieses PHP-Skript nutzen.<\/LI>Versucht habe, stattdessen die IP-Adressen der Besucher zu verwenden. Dies erzeugte ein Token, führte aber zum gleichen Invalid Token-Problem. Meine Organisation kann Anfragen auch von verschiedenen Endpunkten aus senden, was ein möglicher Instabilitätspunkt ist.<\/LI>Versucht habe, den Referer mit anderen PHP-Methoden wie $_SERVER['HTTP_REFERER']; zu setzen.<\/LI>Sie können auch meinen Regex zum Parsen der Tokens sehen – dieser entfernt den Rest der Antwort, so dass nur noch der Token-String übrig bleibt. {".,}. Ich übergebe somit während der Layer-Anfrage kein Ablaufdatum zurück an den Server – ich sehe in keiner Dokumentation, dass dies erforderlich ist.<\/LI><\/UL> <\/P> <\/P> <?php
Was habe ich bisher gemacht? Lassen Sie uns einen gesicherten esri-Demo-Service zur Veranschaulichung verwenden, wobei ich kommentierte Abschnitte belassen habe, in denen ich entweder:<\/P>
<?php
Fixed by treating the json object as json properly.
<SPAN class="delimiter token"><?php</SPAN> <SPAN class="comment token">//Establish our requesting url</SPAN> <SPAN class="keyword token">global</SPAN> <SPAN class="variable token">$wp</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$RequestingPage</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">home_url</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="variable token">$wp</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN><SPAN class="property token">request</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Check to see if the request came from the proper application</SPAN> <SPAN class="comment token">//The url to the token endpoint of the server containing the secure service</SPAN> <SPAN class="variable token">$url</SPAN> <SPAN class="operator token">=</SPAN> "https<SPAN class="punctuation token">:</SPAN><SPAN class="comment token">//someplace.com/arcgis/tokens/";</SPAN> <SPAN class="comment token">//The fields included in the request</SPAN> <SPAN class="variable token">$fields</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">array</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="string token">'request'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'getToken'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'username1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'password1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'expiration'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="number token">120</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'clientid'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'ref.'</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="variable token">$RequestingPage</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="string token">'/'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'f'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'json'</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$fields_string</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">foreach</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$fields</SPAN> <SPAN class="keyword token">as</SPAN> <SPAN class="variable token">$key</SPAN><SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN><SPAN class="variable token">$value</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="variable token">$fields_string</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="operator token">=</SPAN> <SPAN class="variable token">$key</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="string token">'='</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="variable token">$value</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="string token">'&'</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Instantiate Curl</SPAN> <SPAN class="variable token">$ch</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">curl_init</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$url</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_SSL_VERIFYPEER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">false</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="token constant">CURLOPT_POST</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="token function">count</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$fields</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_POSTFIELDS</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="variable token">$fields_string</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_RETURNTRANSFER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">true</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_REFERER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="variable token">$RequestingPage</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$result</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">curl_exec</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//If Curl was not instantiated successfully</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">!</SPAN><SPAN class="token function">curl_exec</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">'An error has occurred: '</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="token function">curl_error</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Otherwise return the result</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">//Treat the response as JSON, and split by attribute.</SPAN> <SPAN class="variable token">$someObject</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">json_decode</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$result</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> '<SPAN class="markup token"><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>script</SPAN> <SPAN class="attr-name token">type</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>text/javascript<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN></SPAN>'<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">'var val = "'</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="variable token">$someObject</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN><SPAN class="property token">token</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="string token">'"'</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> '<SPAN class="markup token"><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>script</SPAN><SPAN class="punctuation token">></SPAN></SPAN></SPAN>'<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Close Curl</SPAN> <SPAN class="token function">curl_close</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="token function">add_action</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="string token">'wp_head'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ple_hook_javascript'</SPAN> <SPAN class="punctuation token">)</SPAN><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></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><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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This works out for me. Thanks Simon
Glad it helped!
I guess what I didn't mention in my initial series of posts was that the script returns the variable "var" which contains the token. When collecting a layer with ESRI-Leaflet you can supply this token as part of the URL. Just for anyone else who happens upon this in the future.
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.