【问题标题】:Cannot get a list of GitHub repositories via Auth0无法通过 Auth0 获取 GitHub 存储库列表
【发布时间】:2022-11-09 00:42:37
【问题描述】:

在用户成功通过 Auth0 进行身份验证后,我试图在我的应用中列出 GitHub 存储库:

import React, { useEffect, useState } from 'react';
import { useAuth0 } from '@auth0/auth0-react';

const Repositories = () => {
  const { getAccessTokenSilently, user } = useAuth0();
  const [repositories, setRepositories] = useState(null);

  useEffect(() => {
    (async () => {
      try {
        const token = await getAccessTokenSilently({
          audience: 'https://api.github.com/',
          scope: 'public_repo',
        });
        const response = await fetch('https://api.github.com/users/' + user.nickname + '/repos', {
          headers: {
            Accept: `application/vnd.github+json`,
            Authorization: `token ${token}`,
          },
        });
        setRepositories(await response.json());
      } catch (e) {
        console.error(e);
      }
    })();
  }, [getAccessTokenSilently]);

  if (!repositories) {
    return <div>Loading...</div>;
  }

  return (
    <ul>
      {repositories.map((repository, index) => {
        return <li key={index}>{repository.name}</li>;
      })}
    </ul>
  );
};

export default Repositories;

但它不起作用。我在控制台Error: Service not found: https://api.github.com/ 中收到错误消息。我怎么了?是否有任何 Auth0 示例显示如何针对 GitHub 进行身份验证?

【问题讨论】:

  • 我有点困惑,为什么使用 Auth0 的身份验证可以让您访问 GitHub。 GitHub 是否支持这样的集成?无论如何,看起来getAccessTokenSilently() 似乎失败了,因为您没有将https://api.github.com/ 注册为Auth0 上的API。

标签: reactjs github auth0


【解决方案1】:

当用户通过 Auth0 登录 GitHub 时,会发布两个访问令牌。

第一的, GH 向 Auth0 发出访问令牌。第二, Auth0 向您的应用程序发出访问令牌。

第一个访问令牌(GH 令牌)与您的用户信息一起存储在 Auth0 数据库中。可以通过用户identities数组中的管理API访问;并被称为身份提供者访问令牌.

该文档对其进行了深入描述:https://auth0.com/docs/secure/tokens/access-tokens/identity-provider-access-tokens

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 2020-12-05
    • 2014-09-21
    相关资源
    最近更新 更多