【问题标题】:Google Play Services: TokenActivity appears empty when asking for authGoogle Play 服务:请求身份验证时 TokenActivity 显示为空
【发布时间】:2013-05-03 07:29:25
【问题描述】:

我正在开发一个使用 Google 云端硬盘作为后端的应用。我已经浏览了 DrEdit 示例,但我遇到了授权问题。我得到了一个UserRecoverableAuthException 并使用它来发布用户必须做某事的通知。问题是当我点击该通知时似乎什么都没有发生。

我说没有“似乎”发生,因为看起来 Google Play 正在启动来处理它,它只是不可见的。如果我点击应用切换按钮,我可以看到具有半透明背景的 Google Play 服务磁贴,但用户永远不会看到身份验证屏幕。

我有什么遗漏吗?我在 API 控制台中有一个项目,配置了驱动器访问权限,并添加了我的发布和开发版本的密钥。

这是我用于通知的代码。这与 DrEdit 示例中的内容几乎完全相同(也有同样的问题)。

try {
    GoogleAccountCredential credential =
            GoogleAccountCredential.usingOAuth2(mContext, DriveScopes.DRIVE_FILE);
    credential.setSelectedAccountName(mAccount.name);
    // Trying to get a token right away to see if we are authorized
    credential.getToken();
    mService = new Drive.Builder(AndroidHttp.newCompatibleTransport(),
            new GsonFactory(), credential).build();
} catch (UserRecoverableAuthException e) {
    Log.e("Failed to get token");
    // If the exception is User Recoverable, we display a notification that will trigger the
    // intent to fix the issue.
    Log.e("Notifying with intent: " + e.getIntent().toString());
    NotificationManager notificationManager = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Intent authorizationIntent = e.getIntent();
    authorizationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(
            Intent.FLAG_FROM_BACKGROUND);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, 
            authorizationIntent, 0);
    Notification notification = new Notification.Builder(mContext)
            .setSmallIcon(android.R.drawable.ic_dialog_alert)
            .setTicker("Permission requested")
            .setContentTitle("Permission requested")
            .setContentText("for account " + mAccount.name)
            .setContentIntent(pendingIntent).setAutoCancel(true).build();
    notificationManager.notify(0, notification);
} catch (Exception e) {
        e.printStackTrace();
}

编辑:

我只是想澄清一下,我确实有一个 Google API 项目设置,可以访问 Drive API(最初是 Drive SDK,但我已修复)以及我的应用程序的调试密钥是使用 keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore -list -v 获取的。还是没有运气。

【问题讨论】:

    标签: android oauth-2.0 google-drive-api google-play-services


    【解决方案1】:

    我终于想通了。看起来文档和示例有点过时了,因为只有 DriveScopes.DRIVE_FILE 作为范围是不够的,它需要在前面加上 "oauth:"

    这是我的代码:

    try {
        // Trying to get a token right away to see if we are authorized
        GoogleAuthUtil.getTokenWithNotification(mContext, mAccount.name, 
                "oauth2:" + DriveScopes.DRIVE_FILE, null, mAuthority, mSyncBundle);
    } catch (UserRecoverableNotifiedException e) {
        Log.e("Failed to get token but notified user");
        return null;
    } catch (Exception e) {
        Log.e("Failed to get token", e);
        return null;
    }
    
    try {
        GoogleAccountCredential credential =
            GoogleAccountCredential.usingOAuth2(mContext, DriveScopes.DRIVE_FILE);
        credential.setSelectedAccountName(mAccount.name);
        ...
    

    注意:我还决定切换到GoogleAuthUtil.getTokenWithNotification,让库为我处理通知,并在完成后重新启动同步适配器。

    【讨论】:

    • 我不明白这个解决方案。能否请您粘贴整个 getDriveService() 方法?
    【解决方案2】:

    这是我使用的 getDriveService 方法。 (复制自 rharter 的回复)

     private Drive getDriveService() {
        if (mService == null) {
            try {
                // Trying to get a token right away to see if we are authorized
                GoogleAuthUtil.getTokenWithNotification(mContext,
                    mAccount.name, "oauth2:" + DriveScopes.DRIVE_FILE,
                    null);//, mAuthority, mSyncBundle);
                } 
            catch (Exception e) {
                Log.e(TAG, "error can't get token GREG GREG GREG", e);
                return null;
            }
          try {
            GoogleAccountCredential credential =
                GoogleAccountCredential.usingOAuth2(mContext, DriveScopes.DRIVE_FILE);
            credential.setSelectedAccountName(mAccount.name);
            // Trying to get a token right away to see if we are authorized
            credential.getToken();
            mService = new Drive.Builder(AndroidHttp.newCompatibleTransport(),
                    new GsonFactory(), credential).build();
                } catch (Exception e) {
                    Log.e(TAG, "Failed to get token");
                    // If the Exception is User Recoverable, we display a notification that will trigger the
                    // intent to fix the issue.
                    if (e instanceof UserRecoverableAuthException) {
                        UserRecoverableAuthException exception = (UserRecoverableAuthException) e;
                        NotificationManager notificationManager = (NotificationManager) mContext
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                        Intent authorizationIntent = exception.getIntent();
                        authorizationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(
                            Intent.FLAG_FROM_BACKGROUND);
                        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
                            authorizationIntent, 0);
                        Notification notification = new Notification.Builder(mContext)
                            .setSmallIcon(android.R.drawable.ic_dialog_alert)
                            .setTicker("Permission requested")
                            .setContentTitle("Permission requested")
                            .setContentText("for account " + mAccount.name)
                            .setContentIntent(pendingIntent).setAutoCancel(true).build();
                        notificationManager.notify(0, notification);
                    } else {
                        e.printStackTrace();
                    }
                }
        }
        return mService;
      }
    

    【讨论】:

      【解决方案3】:

      确保您已在 API 控制台上正确注册了您为应用程序签名的签名。我们的应用程序也有类似的问题,修复签名足迹解决了我们的问题。

      【讨论】:

      • 谢谢,我认为这可能与此有关,但无法解决。我在这里观看了视频:youtube.com/… 以准确设置所有内容。我最初添加了 Drive SDK 服务,修复了这个问题,所以现在它是 Drive API,使用keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore -list -v 来获取调试 SHA1,仍然没有。
      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 2014-05-07
      • 2014-06-21
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      相关资源
      最近更新 更多