如何使用 Google 登录通过 Firebase 登录后访问课堂 API



我创建了一个 Unity 应用程序,可以使用谷歌登录并访问谷歌课堂 API。登录成功,范围也允许访问课程。

问题: 如何在使用 Firebase 登录后查询 Google 课堂 API。 端点 : https://classroom.googleapis.com/v1/courses/105459102203 方法 : 获取 参数:我已经拥有的课程ID

持有者令牌如何从火力基地检索?

当我尝试使用身份验证代码和/或idToken时,它给出了以下错误:

{ "错误":{ "代码":401, "消息": "请求具有无效的身份验证凭据。预期的 OAuth 2 访问令牌、登录 Cookie 或其他有效的身份验证凭据。见 https://developers.google.com/identity/sign-in/web/devconsole-project。 "状态":"未经身份验证" } }

提前谢谢。

有很多方法可以通过Firebase Auth成功发出API请求,特别是Google Classroom API:

  1. 困难的方法是为火力基地创建一个HttpInterceptor。UserCredentials并将其传递给每个HttpRequest的标头,有些像这样:
headers: new HttpHeaders(
{
'Content-Type': 'application/json',
Authorization: `Bearer [${this.user$.AccessToken}]`
})

这就是我所说的困难方式,因为您必须确保在每个 API 服务中传递和刷新令牌。

    使用
  1. JavaScript 库 "gapi" 登录客户端,然后使用令牌响应作为凭据登录 Firebase。此 aproach 会创建一个纯 OAuth2 登录名,用于 Firebase 和进一步的 Google API 请求,如下所示:
declare var gapi;
/** Initialize Google API Client */
initClient(): void {
gapi.client.init({
apiKey: environment.firebaseConfig.apiKey,
clientId: environment.firebaseConfig.clientId,
discoveryDocs: environment.firebaseConfig.discoveryDocs,
scope: environment.firebaseConfig.scope,
});
}
/** Do a OAuth login and then pass it to a FirebaseAuth service */
async login() {
const googleAuth = gapi.auth2.getAuthInstance();
const googleUser = await googleAuth.signIn();
const token = googleUser.getAuthResponse().id_token;
const credential = firebase.auth.GoogleAuthProvider.credential(token);
await this.afAuth().signInAndRetrieveDataWithCredential(credential);
}
/** Then you're ready to make a request*/
/**
* Lists all course names and ids.
* Print the names of the first 10 courses the user has access to. If
* no courses are found an appropriate message is printed.
*/
listCourses() {
this.courses$ = 
gapi.client.classroom.courses.list({pageSize=10;}).then(response => {
return from<Course[]>(response.result.courses);
});
}

最新更新