flutter - 如何通过谷歌登录获取访问令牌——Flutter
问题描述
我想从 google sign 获取访问令牌并将其保存为变量,这样我就可以将它推送到我自己的 API,这个问题之前已经被问过,但答案已经过时了。我正在使用这个包:谷歌登录 她,我通过按钮访问的登录代码:
GestureDetector(
onTap: () {
_googleSignIn.signIn().catchError((e) {
print('Erorr');
});
;
},
解决方案
使用谷歌登录时获取访问令牌
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
完整方法:
import 'package:google_sign_in/google_sign_in.dart';
Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
推荐阅读
- redirect - 如何使用 API 签名的一部分重定向和 API 调用
- java - 如何在准备好的语句中有条件地调用过程?
- matlab - 为什么使用MATPOWER中的Power Flow在线容量会低于发电量(实际)?
- javascript - Material UI 的 TableRow 不允许样式覆盖
- sql - SELECT 一个季节内的日期(任何一年的两个日期之间)
- npm - 从具有语义发布的现有项目开始发布包不是从 1.0.0 开始
- sql - 如何将数组解包到雪花中的行?
- android - 如何在 Android 10 和 Android 11 上从 SD 卡中删除文件夹及其内容
- excel - 如何匹配逗号分隔的列表然后使用索引在excel中获取逗号分隔的结果
- ruby-on-rails - 在 Rails/RSpec 中存根 Dry::Matcher 案例