javascript - 尝试使用 Javascript 从 Reddit 获取图像?

标签 javascript oauth reddit

对于这个特定的任务,我决定最好使用 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)上找到clientIDclientSecret等重要信息。让我困惑的是 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/

相关文章:

javascript - ajax请求时禁用按钮

javascript - 单击提交按钮之前表单不断重定向

javascript - Android与JavaScript、JS如何获取webview JS返回数据?

javascript - jquery悬停和关闭问题

Facebook OAuth 问题?无限循环更改 URL 上的 "state"参数

jquery - 使用 jQuery getJSON 在 Reddit 上获取 url 的分数

jquery - 如何从 Reddit API 加载更多页面?

api - Twitter oauth 请求 token 过期

asp.net - 生成个人访问 token 以调用 Azure Web Api (OAuth)

python - 如何从 Reddit 子版 block 中获取随机帖子(praw)