我正在开发一个使用google drive作为后端的应用程序。我浏览了dredit示例,发现授权有问题。我得到一个UserRecoverableAuthException并用它来发布用户必须做某事的通知。问题是,当我单击该通知时,似乎什么都没有发生。
我说没有什么“看起来”会发生,因为看起来google play正在启动来处理它,只是看不见而已。如果我点击app switch按钮,我可以看到背景半透明的google play服务,但用户永远看不到auth屏幕。
我有什么遗漏吗?我在那个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项目设置,可以访问driveapi(最初是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);
    ...

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

10-07 19:47
查看更多