Firebase身份验证与GoogleAccountCredetial

问题描述 投票:3回答:1

我有一个可以通过GoogleAccountCredential使用Google帐户登录的Android应用,例如,使用Google Spreadsheet API。参见https://developers.google.com/sheets/api/quickstart/android

现在,我想添加也可以与Firebase数据库进行通信的功能,并且我想知道是否有任何方法可以仅使用一种登录方法来进行此操作。参见https://firebase.google.com/docs/auth/android/google-signin

哪个更好?我两个都需要吗?用户是否两次看到“ Google登录对话框”?我可以以某种方式将某些身份验证对象重用于其他方法吗?

android firebase firebase-realtime-database firebase-authentication google-authentication
1个回答
0
投票

[这是一篇旧文章,但我有一些应用可以做到这一点;专门登录到Firebase,然后设置凭据以使用api创建并写入google工作表。此代码假定您已在android项目和开发人员控制台上正确设置了oauth。您也可以在那里为工作表启用api。

完成后,从firebase控制台获取google-services.json并将其放置在您的android项目中。

private static final String[] SCOPES = 
{SheetsScopes.SPREADSHEETS, SheetsScopes.DRIVE_FILE};

// used for spreadsheet access only
    mCredential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(),Arrays.asList(SCOPES));


// set exponential backoff policy
    mCredential.setBackOff(new ExponentialBackOff());

   setCredentailAccount();


private void setCredentailAccount() {

    if (mFirebaseUser != null) {
        mCredential.setSelectedAccount(
                new 
Account(mFirebaseUser.getEmail(),getPackageName()));

        Log.i(TAG, "getAcctPermission - account set");
    } else {
        Log.i(TAG, "getAcctPermission - account not set");
    }
}

我还实现了一个单独的登录活动,该活动封装了该应用程序的登录名。 Firebase登录有很多代码示例。 Google凭据是传递到任务中以执行电子表格工作的东西。另外,Api控制台上的oauth屏幕还包含需要添加的工作表的凭据。

© www.soinside.com 2019 - 2024. All rights reserved.