我正在使用带有Nodejs后端的Angular前端。我目前正在通过我的快递服务器代理我的所有前端请求。但是,当我向Here API发出http请求时,由于app_id和app_code的无效组合,我被拒绝了。
角度服务
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`));
在此之前,我遇到了CORS并提出问题,但我认为我已经解决了这些问题。根据我对相同错误代码的研究(在Im工作的框架中),人们绝大多数建议等待令牌向Here API注册。我认为等待两天就足够了,仍然无法正常工作。然后是一个非常受欢迎的解决方案,它只是刮擦Here免费版并开始一个新项目,而我做到了,但没有解决我的问题。我几乎没有100%把握的事情,但是我确实正确地复制了密钥,并且根据所需的Here语法构建了URL路径。
如果有人有任何见识,您将成为我的英雄,也是我继续学习的催化剂:D。星期天快乐!
此外,我通过快递收到的邮件是:
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'
但是,我不知道为什么要设置app_id =%而不是使用{},当我通过控制台控制台登录URL时,它是正确的,并带有我的app_id和app_code
答案 0 :(得分:0)
%7D是大多数库完成的符号}(urlencoding)的URL编码值。使用HERE API时,请勿在{}之间加上app_id / app_code。它们应直接作为字符串提供,请检查examples