【问题标题】:Reddit API /api/me unable to make it workReddit API /api/me 无法使其工作
【发布时间】:2020-07-12 08:20:43
【问题描述】:

我设法让 Reddit OAuth 工作,它被重定向到我的主页。 我得到一个类似http://localhost:3000/?state=XEA12&code=6p4pAyle2EWGVwIBlFJ6ERXjxKg的网址

现在,当我尝试其他 API 时,例如 /api/me。我无法让它工作。我还将我的应用程序的范围设置为身份。

这是我写的sn-p代码:

import axios from "axios";
import useSWR from "swr";

const link = "https://www.reddit.com/api/v1/me";

const config = {
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    Authorization: "bearer 6p4pAyle2EWGVwIBlFJ6ERXjxKg",
  },
};

const fetcher = (url) => axios.get(url, config).then((res) => res);

export default function Home() {
  const { data, error } = useSWR(link, fetcher);
  if (error) return <div>failed to load </div>;
  if (!data) return <div>loading...</div>;
  return <div>{JSON.stringify(data)}</div>;
}

你能告诉我我做错了什么吗?是因为标题还是我需要传递其他东西?

【问题讨论】:

    标签: javascript reactjs api reddit


    【解决方案1】:

    根据您需要首先检索 access_token 的文档,使用 code 参数。以下是如何做到这一点:https://github.com/reddit-archive/reddit/wiki/OAuth2#retrieving-the-access-token

    然后您可以在不记名身份验证中使用该access_token。还有一个注意事项:带有不记名令牌的 API 请求应该发送到 https://oauth.reddit.com,而不是 www.reddit.com。希望它会有所帮助

    【讨论】:

    • 感谢您的回答,我会尝试实现它,下次会尝试更仔细地阅读文档。
    猜你喜欢
    • 2017-03-10
    • 2020-11-21
    • 2011-12-28
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 2014-02-25
    • 2012-10-30
    • 2013-08-30
    相关资源
    最近更新 更多