【问题标题】:How to retain Google Drive authentication when using Intent.ACTION_VIEW in Android?在 Android 中使用 Intent.ACTION_VIEW 时如何保留 Google Drive 身份验证?
【发布时间】:2016-07-30 15:43:45
【问题描述】:

我在我的应用程序中使用 Google Drive REST api v3。当应用程序启动时,用户将登录到他/她的 Google 帐户并获得身份验证。 当用户启动此意图时

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(googleDriveFileUrl));

假设用户从启动器中选择了 Google Drive 应用程序,现在会弹出登录对话框并要求再次登录。 是否可以让用户使用相同的帐户,以便新活动不会再次要求登录?

【问题讨论】:

    标签: android google-drive-api


    【解决方案1】:

    validate账号authentication有两种方式:

    • 设备上没有注册帐户电子邮件

    GoogleApiClient GAC = new GoogleApiClient.Builder(context) //.setAccountName(email) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(...) .addOnConnectionFailedListener(...) .build();

    Google Play 服务会弹出account picker 对话框供您选择有效帐户或创建新帐户。

    • 在设备上指定有效帐户的电子邮件

    GoogleApiClient GAC = new GoogleApiClient.Builder(context) .setAccountName(email) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(...) .addOnConnectionFailedListener(...) .build();

    您必须通过帐户选择器使用设备的注册设备之一:

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    ...
    static final int REQ_ACCPICK = 999;
    ...
    startActivityForResult(AccountPicker.newChooseAccountIntent(null, null,
    new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null), REQ_ACCPICK);
    ...
    @Override
    protected void onActivityResult(int request, int rslt, Intent data) {
    if (
    request == REQ_ACCPICK &&
    rslt == RESULT_OK && 
    data != null && data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) != null
    )
    email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    

    这是一个示例演示应用程序,其中讨论require sccount pickhttps://github.com/seanpjanson/GDAADemo/blob/master/app/src/main/java/com/spjanson/gdaademo/MainActivity.java

    【讨论】:

    • 很好的信息,但是当我使用这样的意图时仍然无法正常工作 String VideoUrl = "drive.google.com/file/d/FILE_ID/view"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl)); intent.setPackage("com.google.android.apps.docs");开始活动(意图); Google 云端硬盘应用会要求再次选择帐户
    • 如果您找到更好的答案,请将其作为答案发布
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多