尝试使用 Javascript 从 Reddit 获取图像?
Trying to get images from Reddit using Javascript?
对于这个特定的任务,我决定最好使用 Reddit API。查看可用的不同包装器,我选择使用 snoowrap。这些例子很清楚,我想用这样的东西来通过身份验证:
const snoowrap = require('snoowrap');
const otherRequester = new snoowrap({
userAgent: 'put your user-agent string here',
clientId: 'put your client id here',
clientSecret: 'put your client secret here',
username: 'put your username here',
password: 'put your password here'
});
我可以在Reddit Apps (Preferences Section)上找到clientID
和clientSecret
等重要信息。令我困惑的是 userAgent
输入。我到底应该在这里输入什么?
我想我可以转到由同一用户创建的 Reddit OAuth Helper。然而,在这个过程的最后,我似乎得到了一个 Reddit Bad Request。
用户代理是发送请求的浏览器:
您可以使用以下命令填充该数据:
navigator.userAgent
编辑:
以上仅适用于客户端,在服务器端,如果您处于 Nodejs(Expressjs) 环境中,您可以从函数执行的请求参数中的 headers 数据中获取用户代理你的 API 电话。像这样:
app.get('/api-call', function(request, response){
const snoowrap = require('snoowrap');
const otherRequester = new snoowrap({
userAgent: request.headers['user-agent'],
clientId: 'put your client id here',
clientSecret: 'put your client secret here',
username: 'put your username here',
password: 'put your password here'
});
// rest of the code
});
对于这个特定的任务,我决定最好使用 Reddit API。查看可用的不同包装器,我选择使用 snoowrap。这些例子很清楚,我想用这样的东西来通过身份验证:
const snoowrap = require('snoowrap');
const otherRequester = new snoowrap({
userAgent: 'put your user-agent string here',
clientId: 'put your client id here',
clientSecret: 'put your client secret here',
username: 'put your username here',
password: 'put your password here'
});
我可以在Reddit Apps (Preferences Section)上找到clientID
和clientSecret
等重要信息。令我困惑的是 userAgent
输入。我到底应该在这里输入什么?
我想我可以转到由同一用户创建的 Reddit OAuth Helper。然而,在这个过程的最后,我似乎得到了一个 Reddit Bad Request。
用户代理是发送请求的浏览器:
您可以使用以下命令填充该数据:
navigator.userAgent
编辑:
以上仅适用于客户端,在服务器端,如果您处于 Nodejs(Expressjs) 环境中,您可以从函数执行的请求参数中的 headers 数据中获取用户代理你的 API 电话。像这样:
app.get('/api-call', function(request, response){
const snoowrap = require('snoowrap');
const otherRequester = new snoowrap({
userAgent: request.headers['user-agent'],
clientId: 'put your client id here',
clientSecret: 'put your client secret here',
username: 'put your username here',
password: 'put your password here'
});
// rest of the code
});