ArcGIS : Esri 地图 " Invalid Token Used To Access a secure service "



我是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

您正试图在代码中设置一个令牌值,但没有分配任何值,而且变量的名称(CLIENT_SECRET和CLIENT_ID)意味着使用OAuth2登录应用程序。

您有3个访问路由服务的选项,一个用户OAuth2登录,一个应用程序OAuth2登陆或基于令牌的用户登录。首选方法是用户OAuth2登录,这样登录的用户将使用他们的ArcGIS Online命名用户帐户,所需的任何积分都将来自他们的订阅。开发人员门户网站上有一个基于用户的OAuth2登录的代码示例。

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

如果您还没有阅读开发人员门户网站上的身份验证文档,我建议您阅读,因为它对每个选项都有全面的描述。

相关内容

最新更新