Select to view content in your preferred language

IdentityManager, ServerInfo, CORS and tokenServiceUrl confusion

1776
4
03-13-2014 12:52 PM
deleted-user-VeZur_n9o0OZ
Deactivated User
Hello all,

I'm working with the new Spatial Analysis Services. Specifically FindNearest at "http://analysis.arcgis.com/arcgis/rest/services/tasks/GPServer/FindNearest"

When I submit my job to this url by clicking a button I'm prompted to login to AGOL by the IdentityManager. I'd like to either prompt for login when the app loads, or better yet just suppress the sign in all together and login with a developer account I've created.

Using IdentityManager and a ServerInfo object, I can prompt the user to login when the app loads. I then register the returned token with the IdentityManager. However, when I call my FindNearest function I get an error about access control allow origin and the request fails. My app is hosted locally. I've tried adding "analysis.arcgis.com" to esri.config.defaults.io.corsEnabledServers to no avail.

XMLHttpRequest cannot load http://analysis.arcgis.com/arcgis/rest/services/tasks/GPServer/FindNearest/�?�OPERATOR%20LIKE%20%27%.... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. I'm pretty sure analysis.arcgis.com is CORS enabled.

So I'm guessing my ServerInfo is not set up correctly and I'm not generating a valid token/credentials to access analysis.arcgis.com.

            esri.config.defaults.io.useCors = true;
            esri.config.defaults.io.corsEnabledServers.push("analysis.arcgis.com");
            //esriConfig.defaults.io.proxyUrl = "http://localhost/esriProxy/proxy.ashx?"
            esri.config.defaults.io.proxyUrl = "http://localhost/esriProxy/";

            var server = new ServerInfo({
                server: "http://analysis.arcgis.com",
                tokenServiceUrl: "https://www.arcgis.com/sharing/generateToken"
                // I saw the below on my esri.id.server
                //adminTokenServiceUrl: "http://analysis.arcgis.com/arcgis/admin/generateToken"
            })
            var loginDeferred = esri.id.signIn("http://analysis.arcgis.com", server);

            loginDeferred.then(function (response) {
                esri.id.registerToken(response);
            })

$("#searchButton").click(function () {
var gp = new Geoprocessor("http://analysis.arcgis.com/arcgis/rest/services/tasks/GPServer/FindNearest");
var params = //skipped for space but I know these are fine
gp.submitJob(params, completeCallback);

}


I've also tried using a proxy in the event that CORS isn't working but can't seem to crack that either. Still get the CORS error message.

<?xml version="1.0" encoding="utf-8" ?>
<ProxyConfig allowedReferers="*"
             mustMatch="true">
    <serverUrls>
        <serverUrl url="http://services.arcgisonline.com"
                   matchAll="true"/>
  <serverUrl url="http://analysis.arcgis.com"
                   matchAll="true"/>
    </serverUrls>
 
</ProxyConfig>



I'm doing at least one thing wrong...

Cheers,
James
0 Kudos
4 Replies
JeffPace
MVP Alum
i would check a couple things

in firebug/fiddler, is the request actually using the proxy?

also i do something to append my token on some services (ones in a particular folder)

if (url.indexOf("utilities-infrastructure")>-1||url.indexOf("utilitiesinfrastructure")>-1) {
                                    function myCallbackFunction(ioArgs) {
                                        ioArgs.content = ioArgs.content || {};
                                        if (esri.id) {
                                            if (esri.id.credentials[0]) {
                                                if (esri.id.credentials[0].token) {
                                                    ioArgs.content.token = esri.id.credentials[0].token;
                                                }
                                            }
                                        }
                                        return ioArgs;
                                    }
                                    esri.setRequestPreCallback(myCallbackFunction);
                                }
0 Kudos
derekswingley1
Deactivated User
Can you run everything over https? That's how you'll need to run this in production, and you can set up a self-signed cert to do this locally.

When you register your serverInfo, make sure you're using https everywhere. I would also use addProxyRule for analysis.arcgis.com instead of using proxyUrl. The directions sample shows how to set up a proxy for specific resources.
0 Kudos
deleted-user-VeZur_n9o0OZ
Deactivated User
Hi Derek and Jeff,

Thanks for your replies.
Jeff - I can confirm from the proxy is being used.

Derek - I've had some time to try and make your recommended changes. I've created a self signed certificate and and running everything over https locally now. I'm also using addProxyRule. I've posted my code up here: https://deloittegeospatial.github.io/ but don't have a proxy setup (not sure how I'd do that on github). Are you able to get it running locally?

I'm still getting permission errors when I submit the geoprocessing task.

Request URL: https://localhost/esriProxy/proxy.ashx?https://analysis.arcgis.com/arcgis/rest/services/tasks/GPServer/FindNearest/submitJob?f=json&analysisLayer=%7B%22layerDefinition

"You do not have permissions to use this resource."


I actually can't get this running under any scenario now...https/http, proxy/no proxy. Supposed to be demoing it tomorrow :-S

Any help appreciated.
James

PS proxy looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<ProxyConfig allowedReferers="*"
             mustMatch="false">
    <serverUrls>
        <serverUrl url="http://services.arcgisonline.com"
                   matchAll="true"/>
  <serverUrl url="http://analysis.arcgis.com"
                   matchAll="true"/>
    </serverUrls>
 
</ProxyConfig>

<!-- See https://github.com/Esri/resource-proxy for more information -->
0 Kudos
deleted-user-VeZur_n9o0OZ
Deactivated User
OK update...

Got it working locally with https and proxy after doing two things:
1. Manually putting my token on the esriRequest with a content object similar to Jeff's suggestion.

           
 var connectingLines = esriRequest({
                url: url,
                content: { f: "json", token: token}
            });


2. Realising that the trial arcgis online account I'd setup for this app had expired! doh. Hence the permissions warning.

Now I need to figure out how to prompt for login on app load, instead of when I submit the gp job.

http://deloittegeospatial.github.io/

Thank you both.
James
0 Kudos