简体   繁体   中英

Using invalid credentials returns xml instead of json when using Geocoder.geocode()

I'm currently writing a program, that should convert all addresses from a database to its corresponding lat/long coordinates. For this I am using the HERE Geocoding API. Since this program is meant for multiple people I let them enter their app_id and app_code manually. The problem is, that if one of the credentials is invalid it will return XML instead of JSON, which causes an error: (SyntaxError: expected expression, got '<')

This is an example link, which gets called by the geocoder:

http://geocoder.api.here.com/6.2/geocode.json?xnlp=CL_JSMv3.0.17.0&app_id=invalidCredentialID&app_code=InvalidCredentialCode&street=Haupstr.%2042&postalCode=99880&city=Leina&country=DE&jsoncallback=H.service.jsonp.handleResponse(0)

I tried to somehow access the error, but since there's already an error inside the Geocoder I have to wait for the timeout (30s) for my "onError" callback to be called. And even then I don't get an error-code like "invalidCredentials", but just an Object with line and column number.

Basically I'm just using the normal geocoding service

var platform = new H.service.Platform({
  app_id: 'invalidCredentialId',
  app_code: 'invalidCredentialCode'
});
var geocoder = platform.getGeocodingService();

geocoder.geocode({searchText: 'Berlin'}, handleResponse, handleError);

I don't think this works as intended. The Geocoder.geocode() function is expecting a JSON value to pass it on to the callback function, but it's failing since it can't parse XML Data to a JSON object.

If this is working as intended, I would be happy about an advice on how to handle this problem, or maybe a solution to test the validity of given tokens.

It is because the actual error (PermissionError) is wrapped as xml object. In you onError function you have to handle the xml object.

Below is the sample error object:

<ns2:Error xmlns:ns2="http://www.navteq.com/lbsp/Errors/1" type="PermissionError" subtype="InvalidCredentials"><Details>invalid credentials for invalidCredentialId</Details></ns2:Error>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM