首页 > 解决方案 > Here-Maps Routing API 因凭据无效而失败,但其他 Here-API 继续使用完全相同的凭据

问题描述

因此,在设置了一个新的免费增值项目后,我已将https://developer.here.com/documentation/maps/topics/routing.html中的示例代码复制粘贴到一个新项目中,直到最近路由 API 神秘地开始拒绝我的 API 请求:

凭据似乎没问题,因为使用它们的每个 OTHER 请求都可以正常工作。我是否遗漏了什么,或者这是他们的问题?

(网站托管在https://www.cs.drexel.edu/~nim28/CI102/Projects/Project-1/trucking-gps.php

// Instantiate a map and platform object:
var platform = new H.service.Platform({
  'app_id': '4I898D4cYJAYrLryygIZ',
  'app_code': 'lfkO_XjyiIn0D-IdiPw-rg',
  useHTTPS: true
});
// Retrieve the target element for the map:
var targetElement = document.getElementById('map');

// Get the default map types from the platform object:
var defaultLayers = platform.createDefaultLayers();

// Instantiate the map:
var map = new H.Map(targetElement, defaultLayers.normal.map, {
  zoom: 10,
  center: {
    lat: 52.51,
    lng: 13.4
  }
});

// Create the parameters for the routing request:
var routingParameters = {
  // The routing mode:
  'mode': 'fastest;car', // The start point of the route:
  'waypoint0': 'geo!50.1120423728813,8.68340740740811', // The end point of the route:
  'waypoint1': 'geo!52.5309916298853,13.3846220493377', // To retrieve the shape of the route we choose the route
  // representation mode 'display'
  'representation': 'display'
};

// Define a callback function to process the routing response:
var onResult = function(result) {
  var route, routeShape, startPoint, endPoint, linestring;
  console.log(result);
  if (result.response.route) {
    // Pick the first route from the response:
    route = result.response.route[0];
    // Pick the route's shape:
    routeShape = route.shape;

    // Create a linestring to use as a point source for the route line
    linestring = new H.geo.LineString();

    // Push all the points in the shape into the linestring:
    routeShape.forEach(function(point) {
      var parts = point.split(',');
      linestring.pushLatLngAlt(parts[0], parts[1]);
    });

    // Retrieve the mapped positions of the requested waypoints:
    startPoint = route.waypoint[0].mappedPosition;
    endPoint = route.waypoint[1].mappedPosition;

    // Create a polyline to display the route:
    var routeLine = new H.map.Polyline(linestring, {
      style: {
        strokeColor: 'blue',
        lineWidth: 10
      }
    });

    // Create a marker for the start point:
    var startMarker = new H.map.Marker({
      lat: startPoint.latitude,
      lng: startPoint.longitude
    });

    // Create a marker for the end point:
    var endMarker = new H.map.Marker({
      lat: endPoint.latitude,
      lng: endPoint.longitude
    });

    // Add the route polyline and the two markers to the map:
    map.addObjects([routeLine, startMarker, endMarker]);

    // Set the map's viewport to make the whole route visible:
    map.setViewBounds(routeLine.getBounds());
  }
};

// Get an instance of the routing service:
var router = platform.getRoutingService();

// Call calculateRoute() with the routing parameters,
// the callback and an error callback function (called if a
// communication error occurs):
router.calculateRoute(routingParameters, onResult, function(error) {
  alert(error.message);
});
#map {
  height: 50vh;
  width: 75vh;
  margin: auto;
  display: block;
  vertical-align: middle;
  background: gray;
}

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}


/*# sourceMappingURL=trucking-gps.css.map */
<!DOCTYPE html>
<html>

<head>
  <title>Simple Map</title>
  <meta name="viewport" content="initial-scale=1.0">
  <meta charset="utf-8">
  <script src="https://js.api.here.com/v3/3.0/mapsjs-core.js" type="text/javascript" charset="utf-8"></script>
  <script src="https://js.api.here.com/v3/3.0/mapsjs-service.js" type="text/javascript" charset="utf-8"></script>
</head>

<body style="background:gray;">
  <div id="map"></div>
</body>

</html>

编辑


顺便说一句,我确实将这些密钥注册到了正确的域,所以这不是问题。

标签: javascripthere-api

解决方案


免费增值用户需要在他们的请求中设置正确的 HTTP Referer 标头。检查这里的日志,我们可以看到您正在使用:

推荐人= https://www.cs.drexel.edu/~nim28/CI102/Projects/Project-1/trucking-gps.php

但是,交付门户中的推荐人设置为“cs.drexel.edu”(无 www)

这就是您收到 401 错误的原因。因为 app_id / app_code 是正确且有效的,所以 http 401 的唯一原因是引用者错误。我们已经在本地确认将引荐来源网址设置为https://cs.drexel.edu/~nim28/CI102/Projects/Project-1/trucking-gps.php确实有效。

希望这可以帮助!


推荐阅读