重置GoogleAuthUtil,以便再次获得权限

时间:2014-10-07 20:59:54

标签: android google-play-services

我正在使用Google Play服务身份验证示例here

如何重置GoogleAuthUtil以便再次请求权限?

它通过抛出提供给对话框的userRecoverableException来请求权限。但它只需要一次许可。我需要测试再次请求许可。

我已经尝试卸载示例应用并重新安装示例应用,但这不起作用它没有请求权限,似乎它已经知道该应用。

protected String fetchToken() throws IOException {
    try {
        return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
    } catch (UserRecoverableAuthException userRecoverableException) {
        // GooglePlayServices.apk is either old, disabled, or not
        // present, which is
        // recoverable, so we need to show the user some UI through the
        // activity.
        MyGooglePlay.handleException(userRecoverableException);
    } catch (GoogleAuthException fatalException) {
        onError("Unrecoverable error " + fatalException.getMessage(),
                fatalException);
    }
    return null;
}


/**
 * This method is a hook for background threads and async tasks that need to provide the
 * user a response UI when an exception occurs.
 */
public void handleException(final Exception e) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (e instanceof GooglePlayServicesAvailabilityException) {
                // The Google Play services APK is old, disabled, or not present.
                // Show a dialog created by Google Play services that allows
                // the user to update the APK
                int statusCode = ((GooglePlayServicesAvailabilityException)e)
                        .getConnectionStatusCode();
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(statusCode,
                        HelloActivity.this,
                        REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
                dialog.show();
            } else if (e instanceof UserRecoverableAuthException) {
                // Unable to authenticate, such as when the user has not yet granted
                // the app access to the account, but the user can fix this.
                // Forward the user to an activity in Google Play services.
                Intent intent = ((UserRecoverableAuthException)e).getIntent();
                startActivityForResult(intent,
                        REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
            }
        }
    });
}

3 个答案:

答案 0 :(得分:1)

您可以按照以下步骤使用Google Settings应用取消授权已连接的应用:

  1. 启动Google设置应用
  2. Google Settings App Icon

    1. 选择Connected apps选项(位于顶部)
    2. Google Settings options

      1. 显示已连接的应用列表;找到要取消授权的应用并选择它。抱歉没有截图,因为我无法从ATM中删除个人信息 - 但这应该是非常简单的方法:)

      2. 最后,点击应用详情页面上的Disconnect按钮(位于底部)

      3. Connected app details

        请注意,应用程序取消授权可能需要一段时间。

答案 1 :(得分:0)

你也可以调用GoogleAuthUtil.invalidateToken或GoogleAuthUtil.clearToken,这会让它再次获得许可。

答案 2 :(得分:0)

如果您是用户,例如free3dom回答的内容,您可以转到Google设置应用撤消访问权限。

如果您想以编程方式撤消访问权限,可以调用Google的撤销令牌API:https://developers.google.com/identity/protocols/OAuth2WebServer#tokenrevoke。基本上,您应首先通过调用GoogleAuthUtil.getToken()获取带有一组范围的有效令牌,然后撤消令牌。令牌被撤销后,您应该再次看到权限对话框。