对于这个特定的任务,我决定最好使用 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) 环境中,您可以从执行 API 调用的函数的请求参数中的 header 数据中获取用户代理。像这样的事情:
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
});
关于javascript - 尝试使用 Javascript 从 Reddit 获取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46555795/