我正在尝试运行 drive 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;
}
任何想法都会很棒。谢谢。