flutter - 带有 Google 登录帐户的 Google API

标签 flutter dart firebase-authentication google-signin

我已经在我的应用程序中使用 firebase auth 实现了谷歌登录。
我正在尝试使用已登录的 google 帐户实现 GmailAPI,但无法同时找到有关它们的任何信息。
我正在跟进
https://github.com/dart-lang/googleapis/blob/master/generated/googleapis/lib/gmail/v1.dart
并在代码中实现了这一点

final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn(
  scopes: [
      "https://www.googleapis.com/auth/gmail.compose",
      "https://www.googleapis.com/auth/gmail.insert",
      "https://www.googleapis.com/auth/gmail.labels",
      "https://www.googleapis.com/auth/gmail.metadata",
      "https://www.googleapis.com/auth/gmail.modify",
      "https://www.googleapis.com/auth/gmail.readonly",
      "https://www.googleapis.com/auth/gmail.send",
      "https://www.googleapis.com/auth/gmail.settings.basic",
      "https://www.googleapis.com/auth/gmail.settings.sharing",
    ]
);

用户在此之后是否需要身份验证或uid用这个生成的足以验证用户身份吗?
googleAPI 示例在线随处可见,用户已在线进行身份验证,但我找不到 google sign in 的任何内容。 .

最佳答案

我不是 flutter 方面的专家,但基于找到的示例 here您需要以编程方式验证 firebase 的唯一内容是 token_idaccess_token像这样:

final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );

据此,要对 Firebase 进行身份验证,您只需要添加登录范围:'email', 'profile' and 'openid'或范围:'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile' and 'openid' (根据所使用的库,它们通常表示相同)。然后使用生成的访问 token ,您可以使用 firebase 库对 firebase 进行身份验证,而无需重新进行身份验证。
如果您需要更多信息,请与我们联系。

编辑:

您必须找到 Google 登录收到的访问 token 并传递 firebase 登录信息。访问 token 应如下所示:
{
        "access_token": "ya29.a0Adw1xekVMTQRK0_EAdr1PfiOS97Lnz-VCLTJHw7...",
        "expires_in": 3599, 
        "refresh_token": "1//01iBd1CQsBUl2CgYIARAA...", 
        "scope": "https://www.googleapis.com/auth/userinfo.email openid", 
        "token_type": "Bearer", 
        "id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjE31ZmY0ZTEwOTkxZDZiMGA..."
}

关于flutter - 带有 Google 登录帐户的 Google API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61522391/

相关文章:

ios - 从带有应用扩展的 iOS 容器应用共享 Firebase 身份验证 session

flutter - 如何在Flutter中执行分页

ios - 如何修复命令 PhaseScriptExecution 失败并在 macOS 中运行 Flutter 时出现非零退出代码?

flutter - 如何在 flutter 中对齐定位小部件内的文本?

android - 如何在Flutter中对列表进行排序

dart - 'dependencies'字段的值应该是dart中的map

java - 登录时Firebase空指针异常

flutter - 如何在Flutter中获取数据库中的图像下载URL

flutter - 通过单击按钮停止 Flutter 中的 while 循环

firebase - 如何使用 Dart 在 Flutter 中访问 Firestore clearPersistence() 方法