如何使用 Node 获取 Youtube Data API 数据
How to get Youtube Data API data with Node
我正在尝试使用 Node 请求模块从 Youtube 数据 API 获得响应。我可以使用 JQuery 获得我想要的响应,但无法获得我可以使用节点理解的任何数据。
var request=require('request').defaults({ encoding: null });
var ACCESS_TOKEN='*******************';
request.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=ambient&type=video&key='+ACCESS_TOKEN,function(err,header,body){
if (err) throw err
console.log(body);
})
当我控制台记录正文时,它是 'buffer' 类型(不熟悉缓冲区)
如何获取 json 数据?
如果删除 .defaults({ encoding: null })
,您应该得到 json 数据。
var request = require('request');
var ACCESS_TOKEN = '*******************';
request.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=ambient&type=video&key=' + ACCESS_TOKEN, function(err, header, body) {
if (err) throw err
console.log(body);
});
通过将编码设置为 null
,您告诉它 return 它作为 Buffer
。
encoding - Encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)
https://www.npmjs.com/package/request#request-options-callback
自版本 2.88.2 请求包已弃用:截至 2020 年 2 月 11 日,请求已完全弃用。预计土地不会有新变化。事实上,none已经落地一段时间了。
有关为什么不推荐使用 request 和可能的替代方案的更多信息,请参阅 request npm。
我更改为 axios npm,它在服务器端与 NodeJS 一起工作正常。
示例:
axios.get('https://www.some.url').then(response => {
console.log('> response:', response);
}).catch(error => {
console.log(error);
});
或者有一些不同的方法可以在 Node.js 中从 this link
发出 HTTP 请求
我正在尝试使用 Node 请求模块从 Youtube 数据 API 获得响应。我可以使用 JQuery 获得我想要的响应,但无法获得我可以使用节点理解的任何数据。
var request=require('request').defaults({ encoding: null });
var ACCESS_TOKEN='*******************';
request.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=ambient&type=video&key='+ACCESS_TOKEN,function(err,header,body){
if (err) throw err
console.log(body);
})
当我控制台记录正文时,它是 'buffer' 类型(不熟悉缓冲区)
如何获取 json 数据?
如果删除 .defaults({ encoding: null })
,您应该得到 json 数据。
var request = require('request');
var ACCESS_TOKEN = '*******************';
request.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=ambient&type=video&key=' + ACCESS_TOKEN, function(err, header, body) {
if (err) throw err
console.log(body);
});
通过将编码设置为 null
,您告诉它 return 它作为 Buffer
。
encoding - Encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)
https://www.npmjs.com/package/request#request-options-callback
自版本 2.88.2 请求包已弃用:截至 2020 年 2 月 11 日,请求已完全弃用。预计土地不会有新变化。事实上,none已经落地一段时间了。
有关为什么不推荐使用 request 和可能的替代方案的更多信息,请参阅 request npm。
我更改为 axios npm,它在服务器端与 NodeJS 一起工作正常。 示例:
axios.get('https://www.some.url').then(response => {
console.log('> response:', response);
}).catch(error => {
console.log(error);
});
或者有一些不同的方法可以在 Node.js 中从 this link
发出 HTTP 请求