簡體   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