Esta pergunta provavelmente não pertence a um único grupo de desenvolvedores, mas a implementação do Esri-Leaflet é o objetivo final, e provavelmente tem desenvolvedores mais familiarizados com a stack - então aqui vai:<\/P>
<\/P>
Tenho uma situação com um servidor ArcGIS independente e quero acessar serviços de feição seguros. Meu site roda em PHP 7.3 e tem acesso ao cURL. Idealmente, eu gostaria de coletar o token usando credenciais armazenadas em PHP, usando cURL, e então entregar esse token para o Esri-Leaflet quando faço um addtomap, assim mantendo as credenciais seguras. <\/P>
O erro que estou enfrentando na resposta à requisição é o bastante genérico:<\/P>
{"error":{"code":498,"message":"Invalid Token","details":[]}}<\/P><\/BLOCKQUOTE> <\/P>Temporariamente há uma url aqui:<\/P>http:\/\/52.65.144.150\/esri-js-demo-page\/<\/A><\/P> <\/P>Então, o que eu fiz até agora? Vamos usar um serviço demo esri seguro para ilustrar, notando que deixei seções comentadas onde eu:<\/P>Tentei definir o referer manualmente para que o cURL use o mesmo referer que está sendo passado pelo site (esperançosamente). Isso incluiu a página que chama o mapa, visto ao olhar o referer das requisições passando no chrome, e também garantindo que o referer exibido pelo php correspondesse ao que o chrome estava enviando. No geral, não quero usar essa abordagem pois gostaria que múltiplas páginas usassem esse trecho de php.<\/LI>Tentei usar os endereços IP dos visitantes em vez disso. Isso gerou um token mas resultou no mesmo problema de token inválido. Minha organização pode forçar requisições saindo de diferentes endpoints também, então é um possível ponto de instabilidade.<\/LI>Tentei definir o referer usando outros métodos PHP como $_SERVER['HTTP_REFERER'];<\/LI>Você também pode ver meu regex para analisar os tokens - isso apenas remove o resto da resposta para que tudo que reste seja a string do token. {".,}. Assim não estou passando uma expiração de volta ao servidor durante a requisição da camada - não vejo em nenhuma documentação que isso seja necessário.<\/LI><\/UL> <\/P> <\/P> <?php
{"error":{"code":498,"message":"Invalid Token","details":[]}}<\/P><\/BLOCKQUOTE>
Temporariamente há uma url aqui:<\/P>
http:\/\/52.65.144.150\/esri-js-demo-page\/<\/A><\/P> <\/P>Então, o que eu fiz até agora? Vamos usar um serviço demo esri seguro para ilustrar, notando que deixei seções comentadas onde eu:<\/P>Tentei definir o referer manualmente para que o cURL use o mesmo referer que está sendo passado pelo site (esperançosamente). Isso incluiu a página que chama o mapa, visto ao olhar o referer das requisições passando no chrome, e também garantindo que o referer exibido pelo php correspondesse ao que o chrome estava enviando. No geral, não quero usar essa abordagem pois gostaria que múltiplas páginas usassem esse trecho de php.<\/LI>Tentei usar os endereços IP dos visitantes em vez disso. Isso gerou um token mas resultou no mesmo problema de token inválido. Minha organização pode forçar requisições saindo de diferentes endpoints também, então é um possível ponto de instabilidade.<\/LI>Tentei definir o referer usando outros métodos PHP como $_SERVER['HTTP_REFERER'];<\/LI>Você também pode ver meu regex para analisar os tokens - isso apenas remove o resto da resposta para que tudo que reste seja a string do token. {".,}. Assim não estou passando uma expiração de volta ao servidor durante a requisição da camada - não vejo em nenhuma documentação que isso seja necessário.<\/LI><\/UL> <\/P> <\/P> <?php
Então, o que eu fiz até agora? Vamos usar um serviço demo esri seguro para ilustrar, notando que deixei seções comentadas onde eu:<\/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.
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.