我正在构建一个API端点,该端点将使用express.js和Node.js client library for Google Maps API Web Services返回详细信息和一张Google地方的照片.下面是有问题的代码.
对Google Maps客户端有两个请求:
>根据req.params.id获取地点详细信息,并且详细信息包含照片数组
>从上方使用photo_reference获取照片
var googleMapsClient = require('@google/maps').createClient({
key: 'mykeyhere',
Promise: Promise
});
exports.getGglPlace = function(req, res) {
googleMapsClient.place({
placeid: req.params.id
}).asPromise()
.then((response) => {
var venue = response.json.result
if (venue.photos[0]) {
googleMapsClient.placesPhoto({
photoreference: venue.photos[0].photo_reference,
maxwidth: 200
}).asPromise()
.then((photo) => {
console.log("Photo:", photo); // this returns a massive chunk of data which I think contains the actual image object also
venue.photoURL = photo.url; // this doesn't work
res.send(venue);
})
.catch((err)=>{
console.log("Error Getting Photo!", err);
res.send(venue);
})
} else {
res.send(venue);
}
})
.catch((err) => {
res.send(404);
})
}
任何想法如何从响应中获取上面的代码中称为照片的URL?
如果我尝试直接通过浏览器或Postman转到API,则URL将重定向到图像的实际源URL,这就是我要添加到场所对象中的内容.
重定向到(这是我想要photo.url返回的内容):https://lh3.googleusercontent.com/p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
任何帮助表示赞赏.
附言我的第一篇文章-很抱歉,如果我对问题的理解不够清楚.
解决方法:
这是我解决的方法.它似乎并不完美,但是可以完成任务.
响应包含一个req部分,其中包含一些键,我可以使用这些键来构建URL.具体来说,是response.req.socket._host密钥和response.req.path密钥.
这是我的代码中的样子(照片是google API的响应)
place.photoURL =“ https://” photo.req.socket._host photo.req.path;
photo.req.socket._host给了我lh3.googleusercontent.com
photo.req.path给我/ p / AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr = s1600-w200
照片的构造URL的结果是:https://lh3.googleusercontent.com/p/AF1QipPVD12HA5FBnjmiqVLphYgjfPtIPydn4Ie-xGKr=s1600-w200
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。