请求的身份验证范围不足 - 在 Flutter 中使用 Firebase 从 Google 登录获取生日时出错

Posted

技术标签:

【中文标题】请求的身份验证范围不足 - 在 Flutter 中使用 Firebase 从 Google 登录获取生日时出错【英文标题】:Request had insufficient authentication scopes - Error getting birthday from google sign-in using firebase in flutter 【发布时间】:2021-11-12 00:53:31 【问题描述】:

我正在使用 Flutter,我想通过 google 登录获取用户的生日。但是,我只能获取用户的电子邮件 displayName 而不能获取生日。我试图增加从 people API 获取生日的范围,但出现错误。我可以成功获取用户的姓名和电子邮件,但无法获取生日。请帮忙..

这是错误。

W/.pumpit.pump_i(32752):Accessing hidden method Lsun/misc/Unsafe; >compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed)
I/flutter (32752): loading birthday
I/flutter (32752): loading completed
I/flutter (32752): api error
I/flutter (32752): 
I/flutter (32752):   "error": 
I/flutter (32752):     "code": 403,
I/flutter (32752):     "message": "Request had insufficient authentication scopes.",
I/flutter (32752):     "status": "PERMISSION_DENIED"
I/flutter (32752):   
I/flutter (32752): 

这是我的代码:

var googleIdToken;
FirebaseUser user;

Future<FirebaseUser> signInWithGoogle() async 

final GoogleSignIn _googleSignIn = GoogleSignIn(
    scopes: ['https://www.googleapis.com/auth/user.birthday.read']
);

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

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

googleIdToken = googleSignInAuthentication.idToken;

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

final host = "https://people.googleapis.com";
final endpoint= "/v1/people/me/connections?personFields=names,birthdays";
final header = await googleSignInAccount.authHeaders;

print('loading birthday');
final request = await http.get("$host$endpoint", headers: header);
print('loading completed');

if(request.statusCode == 200)
  print('api working perfectly');
  print(request.body);
 else 
  print('api error');
  print(request.body);


// connecting to mongo database
AuthService().googleLogin(googleIdToken).then((val) 

  if (val.data['success']) 
     var token2 = val.data['token'];

     AuthService().getInfo(token2).then((val) async 
       print('SUCCESS AUTHENTICATION');
       var store = val.data['user'];

      showDialog(context: context,
          builder: (BuildContext context)
            return  _SuucessLogin();
          );
      EasyLoading.dismiss();

     );
   else 
    print('WRONG EMAIL/ PASS');
  
);

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

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

return user;

有人可以帮助我吗?提前谢谢!

【问题讨论】:

更改范围后是否获得了新令牌? @nit21 【参考方案1】:

您应该使用 GetPerson 操作来获取经过身份验证的用户,而不是使用 ListConnections 来获取联系人。见文档https://developers.google.com/people/v1/profiles#get_the_person_for_the_authenticated_user

【讨论】:

我会检查的。谢谢!

以上是关于请求的身份验证范围不足 - 在 Flutter 中使用 Firebase 从 Google 登录获取生日时出错的主要内容,如果未能解决你的问题,请参考以下文章

请求的身份验证范围不足 javascript

Google Calendar API - 请求的身份验证范围不足

Gmail Api 请求的身份验证范围不足

Youtube 评论 API 抛出“权限不足:请求的身份验证范围不足”错误

错误 403:“请求的身份验证范围不足”,即使相关范围已激活

请求的身份验证范围不足 - GCP 上的 Dataflow/Apache Beam