【问题标题】:Flutter can't get data from Google APIFlutter 无法从 Google API 获取数据
【发布时间】:2020-02-27 13:58:35
【问题描述】:

我正在尝试访问 Google People API 以获取一些用户数据,例如姓名、电子邮件等。我使用 google_sign_in 插件和 firebase_auth 通过 Google Sign-in 登录用户,然后我想在那里提到他的数据并将其保存到我的数据库中。问题是我尝试过的一切似乎都不起作用,我真的找不到更多关于这个的信息。您现在必须将 google_sign_in 与 firebase_auth 一起使用并且不能在没有 Firebase 的情况下使用它,这也是真的吗?因为我只需要来自 People API 的 userData。

我在堆栈上找到的一些“解决方案”不起作用,其中一个是 How to use Google API in flutter? 我也尝试过使用此代码https://github.com/flutter/plugins/blob/master/packages/google_sign_in/example/lib/main.dart,但它没有 Firebase,我看到人们说你现在必须将它与 Firebase 一起使用。

import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis/people/v1.dart';
import 'package:http/http.dart'
    show BaseRequest, IOClient, Response, StreamedResponse, get;
import 'package:http/io_client.dart';

import 'package:google_sign_in/google_sign_in.dart'
    show GoogleSignIn, GoogleSignInAccount;

import 'package:googleapis/people/v1.dart'
    show ListConnectionsResponse, PeopleApi;


final FirebaseAuth _auth = FirebaseAuth.instance;

final GoogleSignIn googleSignIn = GoogleSignIn(
  scopes: <String>['https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/userinfo.email'],
);



Future<String> signInWithGoogle() async {

  final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
  final GoogleSignInAuthentication googleSignInAuthentication =
  await googleSignInAccount.authentication;

  final authHeaders = googleSignIn.currentUser.authHeaders;






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

  final AuthResult authResult = await _auth.signInWithCredential(credential);
  final FirebaseUser user = authResult.user;

  assert(!user.isAnonymous);
  assert(await user.getIdToken() != null);

  final FirebaseUser currentUser = await _auth.currentUser();
  assert(user.uid == currentUser.uid);

  return 'signInWithGoogle succeeded: $user';

}

void signOutGoogle() async{
  await googleSignIn.signOut();

  print("User Sign Out");
}



class GoogleHttpClient extends IOClient {
  Map<String, String> _headers;

  GoogleHttpClient(this._headers) : super();

  @override
  Future<StreamedResponse> send(BaseRequest request) =>
      super.send(request..headers.addAll(_headers));

  @override
  Future<Response> head(Object url, {Map<String, String> headers}) =>
      super.head(url, headers: headers..addAll(_headers));

}

除了简单地显示“使用 Google 登录”对话框外,用户单击它,我从 People API 获取他的数据并将其发送到我的后端,并从他的数据创建一个用户实例。基本上就是这样:)

【问题讨论】:

    标签: firebase dart google-api google-signin flutter-dependencies


    【解决方案1】:

    我希望你能解释一些代码:)

    here 这个家伙做了一个完美的 Google 登录示例。

    Google People Api 用于用户联系人,因此您需要 thisthis 包。

    现在需要 google_sign_in 和 firebase_auth?

    是的,然后您通过以下方式将 google 登录链接到 firebase_auth 用户:

    await authInstance.signInWithCredential(credential);
    

    【讨论】:

    • 嗨,马克!感谢您的回答,我看到我可以获取个人资料和一些用户的数据,但是我是否使用我获得的 uid 向 People API 发出获取请求,或者我是否以某种方式使用了 googleapis 插件?我还需要在 API 控制台中注册新应用吗?我已经注册了一个,但我不知道授权域的链接是什么。谢谢!
    • 我询问注册应用程序的原因是因为 googleapis 插件需要 api 凭据 final _credentials = new ServiceAccountCredentials.fromJson(r''' { "private_key_id": ..., "private_key ": ..., "client_email": ..., "client_id": ..., "type": "service_account" } ''');
    猜你喜欢
    • 2019-08-26
    • 2021-08-20
    • 2020-11-16
    • 2021-06-10
    • 2021-06-20
    • 2021-07-20
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多