首页 > 解决方案 > 谷歌课堂 API 的访问令牌表示它已授权课堂 api,但其余 api 返回请求缺少凭据

问题描述

我有一个 react-native 应用程序,它使用 firebase web SDK 登录用户。应用程序成功登录用户,登录时,系统会提示用户使用以下范围授权 google 课堂 api: ['profile', 'email', "https://www.googleapis.com/auth/classroom.courses", "https://www.googleapis.com/auth/classroom.courses.readonly"]

成功验证后,用户将被重定向到仪表板。在此仪表板中,我想显示他们注册的 google 教室。我将访问令牌存储在 firebase 中,但是当我从 google 教室 rest api 获取时,我收到一条错误消息,指出请求缺少凭据。

我已经尝试直接在标头和 url 中包含访问令牌。

我如何让我的用户登录:

(onSignIn 使用他们提供的 firebase 手动代码手动将用户登录到 firebase)

googleAuth = async () => {
    try {
      const result = await Expo.Google.logInAsync({
        iosClientId : "MY IOS CLIENT ID IS ACTUALLY SET IN MY CODE",
        scopes : ['profile', 'email', "https://www.googleapis.com/auth/classroom.courses", "https://www.googleapis.com/auth/classroom.courses.readonly"],
        behavior : 'web'
      })

      if(result.type === 'success') {
        this.onSignIn(result)
        console.log(result)
      }
    }
    catch(e) {
      console.log("ERROR", e)
    }
  }

一旦路由到仪表板:

listCourses = async (auth) => {
    if(firebase.auth().currentUser) {

      const token = await firebase.auth().currentUser.getIdToken()
      .then(tkn => {
        return tkn
      })
      .catch(err => {
        console.log(err)
        return undefined;
      })

      if(token) {
        fetch("https://classroom.googleapis.com/v1/courses?accessToken=" + token, {
          method : "GET",
        })
        .catch(err => {
          console.log(err)
        })
        .then(res => {
          console.log(res)
        })
      }
    }
  }

我期望得到一个类列表,类似于我在谷歌课堂休息 API 上使用 Oauth2.0 客户端进行身份验证时发生的情况(我收到了一个包含数组中课程的 200 请求)实际发生的是我收到了这个错误:

401 error
\"message\": \"Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.\",
"url": "https://classroom.googleapis.com/v1/courses?accessToken=MY ACTUAL ACCESS TOKEN",

标签: react-nativeoauthgoogle-apis-explorergoogle-classroom

解决方案


ID 令牌和访问令牌是不同的东西。ID 令牌包含 JWT 格式的用户信息,而访问令牌是您与未来请求一起发送的不透明字符串。有关使用 Google 登录 Firebase 的更多信息,请访问此处:

https://firebase.google.com/docs/auth/web/google-signin


推荐阅读