Google Play 服务:请求身份验证时 TokenActivity 显示为空

2024-01-21

我正在开发一个使用 Google Drive 作为后端的应用程序。我已经运行了 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();
}

Edit:

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


我终于弄清楚了这一点。看起来文档和示例有点过时了,因为只是有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);
    ...

Note:我也决定转行GoogleAuthUtil.getTokenWithNotification让库为我处理通知并在完成后重新启动同步适配器。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Play 服务:请求身份验证时 TokenActivity 显示为空 的相关文章

随机推荐