Search code examples
expressrequesthere-api

Here-API 401 : "Invalid app_id app_code combination"


I am using an Angular front-end with a Nodejs backend. Im currently proxying all my front-end requests through my express server. However when I make my http request to the Here API I am rejected due to an invalid combination of app_id and app_code.

angular service

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { HttpParams } from '@angular/common/http'

@Injectable({
  providedIn: 'root'
})

export class GetReqPlaces {

  constructor(private http: HttpClient) { }

  getPlaces(wLong,sLat,eLong,nLat){

    // let obj = {params: {westLong: wLong, southLat: sLat, eastLong:eLong, northLat:nLat }};

    let params = new HttpParams().set("westLong" , '-97.783').set("southLat", '30.231').set( "eastLong" , '-97.740').set("northLat", '30.329');

    return this.http.get( 'api/find/places', { params : params}).subscribe(res=>console.log(res))

  }
}

server.js

const express = require("express") 
const bodyParser = require("body-parser")
const cors = require("cors")
const path = require("path")
const app = express();
const request = require("request")
const environment= require('./keys')


app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

let reqPath = __dirname.substring(0,__dirname.length-7)

app.use(express.static(path.join(reqPath, '/dist/angular-places-search')));

app.get('/api/find/places', (req, res) => { 

   let appId = environment.environment.appId;
   let appCode = environment.environment.appCode;
   let URL= `https://places.cit.api.here.com/places/v1/discover/search?app_id={${appId}}&app_code={${appCode}}&in=${req.query.westLong},${req.query.southLat},${req.query.eastLong},${req.query.northLat}&pretty`;

   console.log(URL)

   request(URL, function (error, response, body) {
        let data={
          body:body,
        };
      console.log(error,response)
      res.send(data);

      });
});


app.get('/test', (req, res) => res.send('Well this route was a hit! Bada....tsss'));

// CATCH ALL
app.get('*', (req, res) => {
    res.sendFile(path.join(reqPath, 'dist/angular-places-search/index.html'));
  });

app.listen(4000, () => console.log(`Express server running on port 4000`));

Before this I was running into CORS and request issues but I think I sorted those out. Based on my research on this same error code (In the context of the framework that Im working in), people overwhelmingly suggest to wait for tokens to register with Here API. Waiting two days is enough I think, still doesnt work. Then there is the very popular solution of just scratching the Here freemium and starting a new project, which I did, and which did not solve my issue. Very few things I have 100% certainty on but I did copy my keys correctly and the URL path built is according to the required Here syntax.

If anyone has any insight you will be my Hero, and also the catalyst for my continued learning :D. Happy Sunday!

In addition the incoming message I get through express is :

 method: 'GET',
 path: '/places/v1/discover/search?app_id=%notmyid%7D&app_code=%normycode%7D&in=-97.783,30.231,-97.740,30.329&pretty'

However i dont know why it is setting the app_id=% instead of using {}, when i console log the URL it is correct, with my app_id and app_code


Solution

  • The %7D is the url encoded value of the symbol } (urlencoding) which is done by most libraries. For using the HERE API you should not enclose the app_id/app_code between {}. They should be provided directly as strings, check the examples