【问题标题】:ArcGis : Esri Map " Invalid Token Used To Access a secure service "ArcGis:Esri 地图“用于访问安全服务的令牌无效”
【发布时间】:2015-06-26 20:59:16
【问题描述】:

我是 arcgis 和 esri 地图的新手,我正在尝试在两点之间建立路线, 这是我的代码:

map.setOnLongPressListener(new OnLongPressListener() {

        private static final long serialVersionUID = 1L;

        public boolean onLongPress(final float x, final float y) {

            grahpicslayer.removeAll();

            final Point loc = map.toMapPoint(x, y);


            grahpicslayer.addGraphic(new Graphic(loc, new SimpleMarkerSymbol(Color.RED, 20, SimpleMarkerSymbol.STYLE.CIRCLE)));

            //Display the red color diamond graphics
            map.addLayer(grahpicslayer);

            //zoom the map to the location
            map.zoomToResolution(loc, 20.0);


            new Thread(new Runnable() {
                @Override
                public void run() {


                    // new code
                    try {

                        String CLIENT_SECRET = "";
                        String CLIENT_ID = "";


                        //Prepare user credentials
                        UserCredentials userCredentials = new UserCredentials();
                        userCredentials.setAuthenticationType(UserCredentials.AuthenticationType.TOKEN);
                        userCredentials.setSSLRequired(false);
                        userCredentials.setUserToken(CLIENT_ID, CLIENT_SECRET);



            /*initialize RouteTask*/
                        String routeTaskURL = "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
                        RouteTask rt = RouteTask.createOnlineRouteTask(routeTaskURL, userCredentials);
                        RouteParameters rp = rt.retrieveDefaultRouteTaskParameters();
                        NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature();

                        // Convert point to EGS (decimal degrees)
                        Point p = (Point) GeometryEngine.project(loc, wm,
                                egs);

                        //mlocation is your latitude and longitude point provided by GPS location in decimal degrees
                        StopGraphic point1 = new StopGraphic(mLocation);
                        StopGraphic point2 = new StopGraphic(p);

                        String message = "point 1 == " + ((point1 == null) ? "null" : point1.getName()) + "point 2 == " + ((point2 == null) ? "null" : point2.getName());

                        Message msg = Message.obtain(); // Creates an new Message instance
                        msg.obj = message; // Put the string into Message, into "obj" field.
                        msg.setTarget(h); // Set the Handler
                        msg.sendToTarget(); //Send the message


                        rfaf.setFeatures(new Graphic[]{point1, point2});

                        rfaf.setCompressedRequest(true);
                        rp.setStops(rfaf);
                        rp.setOutSpatialReference(wm);

                        RouteResult mresults = rt.solve(rp);

                    } catch (Exception e) {

                        String message = "ex : " + e.toString();

                        Message msg = Message.obtain(); // Creates an new Message instance
                        msg.obj = message; // Put the string into Message, into "obj" field.
                        msg.setTarget(h); // Set the Handler
                        msg.sendToTarget(); //Send the message
                    }
                }
            }).start();
            return true;
        }

    });

根据位置变化监听器获取当前位置的代码

/**
     * If location changes, update our current location. If being found for
     * the first time, zoom to our current position with a resolution of 20
     */
    public void onLocationChanged(Location loc) {
        if (loc == null)
            return;
        boolean zoomToMe = (mLocation == null) ? true : false;
        mLocation = new Point(loc.getLongitude(), loc.getLatitude());
        if (zoomToMe) {
            Point p = (Point) GeometryEngine.project(mLocation, egs, wm);
            map.zoomToResolution(p, 20.0);
        }
    }

运行此代码后,我遇到此错误:

com.esri.core.io.EsriSecurityException : 用于访问安全服务的令牌无效 - https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World

【问题讨论】:

标签: android arcgis esri


【解决方案1】:

您尝试在代码中设置令牌值,但未分配任何值,变量名称(CLIENT_SECRET 和 CLIENT_ID)也暗示使用 OAuth2 登录应用程序。

您有 3 个选项用于访问路由服务,用户 OAuth2 登录、应用 OAuth2 登录或基于令牌的用户登录。首选方法是用户 OAuth2 登录,这样登录的用户将使用他们的 ArcGIS Online 指定用户帐户,并且所需的任何积分都来自他们的订阅。开发人员门户上有一个code sample 用于用户群 OAuth2 登录。

如果您仍想使用基于令牌的登录,则需要为 UserCredential 设置用户名和密码,以便生成令牌或使用您已经调用但传入有效令牌字符串的方法。

如果您还没有阅读开发者门户上的authentication documentation,我建议您阅读它,因为它提供了每个选项的全面描述。

【讨论】:

  • 亲爱的戴夫,我把(CLIENT_SECRET 和 CLIENT_ID)留空了,因为我认为它不应该被分享“如果你想让我分享给你,我可以做到”。关于 OAUth2,我认为让用户登录到 arcgis 以使用用户应该能够在不登录的情况下使用此服务的路由不是逻辑,对吗? ,如果正确,我该怎么办。
  • 不,该服务需要指定用户才能访问它,因为它是一项高级服务,您可以在resources.arcgis.com/en/help/arcgis-rest-api/#/… 看到访问信息。回到您的示例, CLIENT_SECRET 和 CLIENT_ID 表示应用程序登录,但您正在调用 setToken 这意味着您有一个令牌,但您没有,所以您需要确保选择我上面提到的 3 种身份验证方法之一。
  • 那么,如果我想在应用程序中制作路线而不需要用户以普通路线图登录,我该怎么办
  • 您可以使用 3 种身份验证方法中的任何一种,但它们都需要某种形式的硬编码。如果您不希望用户自己登录,最有意义的是应用程序登录,因此使用您的 CLIENT_ID 和 SECRET 但您没有将令牌设置为这些值,您需要使用 OAuth2 端点生成它然后使用调用路由服务时返回的访问令牌。请参阅developers.arcgis.com/authentication/… 了解更多信息。另外请注意,这会消耗您开发者帐户的积分。
  • 你的意思是路由服务是付费服务吗
猜你喜欢
  • 2015-10-11
  • 2012-09-11
  • 2019-07-23
  • 2014-03-14
  • 1970-01-01
  • 2021-12-26
  • 2018-12-26
  • 2016-05-15
  • 1970-01-01
相关资源
最近更新 更多