无法获得Token DrEdit android示例

时间:2013-04-23 22:16:25

标签: android google-drive-api

我正在尝试运行驱动程序sdk android sample DrEdit,但我得到一个例外:

04-23 16:49:19.642: E/DriveSyncAdapter(11914): Failed to get token
04-23 16:49:19.642: W/System.err(11914): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
04-23 16:49:19.642: W/System.err(11914):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
04-23 16:49:19.642: W/System.err(11914):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
04-23 16:49:19.642: W/System.err(11914):    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
04-23 16:49:19.642: W/System.err(11914):    at com.example.android.notepad.DriveSyncer.getDriveService(DriveSyncer.java:381)
04-23 16:49:19.642: W/System.err(11914):    at com.example.android.notepad.DriveSyncer.<init>(DriveSyncer.java:94)
04-23 16:49:19.642: W/System.err(11914):    at com.example.android.notepad.DriveSyncAdapter.onPerformSync(DriveSyncAdapter.java:32)
04-23 16:49:19.642: W/System.err(11914):    at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:254)

在:

credential.getToken();

在DriveSyncer.getDriveService()。

我已经在清单中更改了我的ID,并设置了适当的权限:

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_SYNC_STATS" />
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
   ...
    <meta-data
      android:name="com.google.android.apps.drive.APP_ID"
      android:value="id=XXXXXXXX.apps.googleusercontent.com" />

还启用了驱动器API并设置了凭据。

在我的Android设备中,它会询问我该帐户但从未要求其他授权,它会显示通知,但就是全部。但是,快速入门示例运行完美。

我收到错误的代码是:

      private Drive getDriveService() {
        if (mService == 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();
            Log.e("MyTag", "Never Called");
            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) 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;
      }

任何想法都会很棒。感谢。

1 个答案:

答案 0 :(得分:2)

即使我只运行原始的DrEdit,我也有同样的问题。权限请求显示在通知栏上,但单击后没有响应。

PS。我使用HTC 4.0.1和谷歌手机试试这个问题,但没有人工作。

无论如何,这篇文章解决了我的问题,你可以尝试一下。 https://stackoverflow.com/a/16535080/1645840

唯一不同的是我调用了这个函数,它仍然有效。

GoogleAuthUtil.getTokenWithNotification(mContext,
                    mAccount.name, "oauth2:" + DriveScopes.DRIVE_FILE,
                    null);//, mAuthority, mSyncBundle);