【问题标题】:Trying to get images from Reddit using Javascript?尝试使用 Javascript 从 Reddit 获取图像?
【发布时间】:2018-03-15 07:29:22
【问题描述】:

对于这个特定的任务,我决定最好使用 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 错误请求

【问题讨论】:

  • 对于userAgent,您需要在其中放置一个用户代理字符串。
  • 我的用户代理字符串到底是什么,我在哪里可以找到它? @SvenWritesCode
  • 我真的不是要粗鲁,但如果你用谷歌搜索“我的用户代理字符串到底是什么”,标题为 What's My User Agent? - User Agent & Browser Tools 的第一个网站会告诉你。
  • @SvenWritesCode 是的,我看过了,但那是我的机器特有的,我希望它适用于所有机器。

标签: javascript oauth reddit


【解决方案1】:

用户代理是发送请求的浏览器:

您可以使用以下命令填充该数据:

navigator.userAgent

编辑:

以上内容仅适用于客户端,如果您在 Nodejs(Expressjs) 环境中,则可以在服务器端从执行 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
});

【讨论】:

  • 我似乎收到了错误:Error: Cannot find module 'navigator'。我应该在这里做什么?
  • 你是在客户端还是服务器端执行这个函数?
  • 在服务器端
猜你喜欢
  • 2020-12-16
  • 1970-01-01
  • 2015-06-08
  • 2019-10-16
  • 1970-01-01
  • 2017-01-04
  • 2014-12-11
  • 2012-09-12
  • 1970-01-01
相关资源
最近更新 更多