繁体   English   中英

如何在 flutter 中获取谷歌登录访问令牌

[英]How to get google sign in access token in flutter

我想从谷歌登录获取访问令牌。我已经进行了谷歌登录编码,但我只得到了 displayName、email、id、photoUrl。 我可以知道如何获取访问令牌吗?

这是我的代码:

GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: <String>[
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
  ],
);

class SignInDemo extends StatefulWidget {
  @override
  State createState() => SignInDemoState();
}

class SignInDemoState extends State<SignInDemo> {
  GoogleSignInAccount _currentUser;
  String _contactText;

  @override
  void initState() {
    super.initState();
    _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {
      setState(() {
        _currentUser = account;
      });
    });
  }


  Future<void> _handleSignIn() async {
    try {
      var user = await _googleSignIn.signIn();
      print(user);
    } catch (error) {
      print(error);
    }
  }

  Future<void> _handleSignOut() => _googleSignIn.disconnect();
}

您可以使用GoogleSignInAuthentication class 获取访问令牌;

Future<void> _handleSignIn() async {
  try {
    GoogleSignInAccount user = await _googleSignIn.signIn();

    GoogleSignInAuthentication googleSignInAuthentication = await user.authentication;

    print(googleSignInAuthentication.accessToken);


  } catch (error) {
    print(error);
  }
}

你可以试试这个

 _googleSignIn.signIn().then((result){ result.authentication.then((googleKey){ print(googleKey.accessToken); print(googleKey.idToken); }).catchError((err){ print('Error occured inside'); }); }).catchError((err){ print('Error occured outside'); });

享受编码!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM