我正在尝试添加对 new Google Sign-in 的支持作为 Play 服务 8.3.0 的一部分宣布。
我成功配置了项目,并且从 GoogleApiClient 获得了一个 token ,但 Firebase 正在返回一个
无效凭据错误
打电话时
ref.authWithOAuthToken("google", token)
Google+ 登录工作正常,但需要单独的许可,这在为 Marshmallow 开发时很麻烦。 Firebase android tutorial有一个 Google+ 登录示例,我的感觉是他们还不支持新的 Google 登录。
有没有人尝试过与 Firebase 相关的新 Google 登录并让它工作?
最佳答案
它是Add Sign-In to Android 中步骤的混合。和 Authorizing with Google for REST APIs .
一旦您拥有 GoogleSignInResult
,您就可以从中获取帐户名称,然后请求具有最小范围的 token :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
String email = acct.getEmail();
// TODO: run an async task to get an OAuth2 token for the account
}
}
异步任务需要请求这些范围:
protected String doInBackground(String... params) {
String scopes = "oauth2:profile email";
String token = GoogleAuthUtil.getToken(getApplicationContext(), email, scopes);
// exception handling removed for brevity
return token;
}
现在您可以像往常一样使用 token 登录 Firebase:
ref.authWithOAuthToken("google", token, new Firebase.AuthResultHandler() {...
关于android - Firebase 和 Android 上的新 Google 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33631750/