我有一個從服務器接收的Google Drive訪問令牌。收到我之後,嘗試使用此令牌初始化GoogleCredential
,然後對Drive API執行請求。但是,當我嘗試執行請求我趕上一個堆棧跟蹤:Android GoogleCredential無效憑據
04-13 15:23:22.715 29184-29570/com.rcd.perfecto W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
04-13 15:23:22.715 29184-29570/com.rcd.perfecto W/System.err: {
04-13 15:23:22.715 29184-29570/com.rcd.perfecto W/System.err: "code" : 401,
04-13 15:23:22.715 29184-29570/com.rcd.perfecto W/System.err: "errors" : [ {
04-13 15:23:22.715 29184-29570/com.rcd.perfecto W/System.err: "domain" : "global",
04-13 15:23:22.715 29184-29570/com.rcd.perfecto W/System.err: "location" : "Authorization",
04-13 15:23:22.715 29184-29570/com.rcd.perfecto W/System.err: "locationType" : "header",
04-13 15:23:22.715 29184-29570/com.rcd.perfecto W/System.err: "message" : "Invalid Credentials",
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: "reason" : "authError"
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: } ],
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: "message" : "Invalid Credentials"
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: }
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.utils.storages.helpers.GoogleDriveHelper.getQuotaInfo(GoogleDriveHelper.java:163)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.utils.storages.helpers.GoogleDriveHelper.addCloud(GoogleDriveHelper.java:100)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.utils.storages.helpers.GoogleDriveHelper.initClient(GoogleDriveHelper.java:93)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.utils.storages.helpers.GoogleDriveHelper.<init>(GoogleDriveHelper.java:53)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.utils.storages.helpers.GoogleDriveHelper.getInstance(GoogleDriveHelper.java:107)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.utils.storages.helpers.StoragesHelper.getGoogleDriveHelper(StoragesHelper.java:135)
04-13 15:23:22.716 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.utils.storages.helpers.StoragesHelper.initClients(StoragesHelper.java:97)
04-13 15:23:22.717 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.utils.CloudStorageCollector.saveCloudsToDatabase(CloudStorageCollector.java:61)
04-13 15:23:22.717 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.services.SyncApiService.getOldCloudsSync(SyncApiService.java:205)
04-13 15:23:22.717 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.services.SyncApiService.startCloudSync(SyncApiService.java:194)
04-13 15:23:22.717 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.services.SyncApiService.handleActionFullSync(SyncApiService.java:176)
04-13 15:23:22.717 29184-29570/com.rcd.perfecto W/System.err: at com.rcd.perfecto.services.SyncApiService.onHandleIntent(SyncApiService.java:153)
04-13 15:23:22.717 29184-29570/com.rcd.perfecto W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
04-13 15:23:22.717 29184-29570/com.rcd.perfecto W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 15:23:22.718 29184-29570/com.rcd.perfecto W/System.err: at android.os.Looper.loop(Looper.java:148)
04-13 15:23:22.718 29184-29570/com.rcd.perfecto W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
還有就是我如何初始化憑據,並執行請求驅動:
private void initClient() {
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport transport = AndroidHttp.newCompatibleTransport();
try {
GoogleClientSecrets googleClientSecrets = loadClientSecretsResource(mContext, jsonFactory);
GoogleCredential credential = new GoogleCredential.Builder()
.setClientSecrets(googleClientSecrets)
.setJsonFactory(jsonFactory)
.setTransport(transport)
.build();
credential.setAccessToken(AppPreferences.Storage.GoogleDrive.getToken(mContext));
credential.refreshToken();
mService = new Drive.Builder(transport, jsonFactory, credential)
.setApplicationName(mContext.getResources().getString(R.string.app_name))
.build();
addCloud();
} catch (IOException e) {
e.printStackTrace();
}
}
private void addCloud() {
Map<String, Long> quotaInfo = getQuotaInfo();
CloudStorageCollector.saveCloudToDatabase(AppPreferences.Storage.GoogleDrive.getToken(mContext), BaseVault.VaultCode.GoogleDriveVault,
quotaInfo.get(StoragesHelper.USED_QUOTA_KEY), quotaInfo.get(StoragesHelper.TOTAL_QUOTA_KEY));
}
public Map<String, Long> getQuotaInfo() {
if (mService == null) return null;
Map<String, Long> quotaInfo = new HashMap<>();
try {
About about = mService.about().get().execute();
quotaInfo.put(StoragesHelper.USED_QUOTA_KEY, about.getQuotaBytesUsed());
quotaInfo.put(StoragesHelper.TOTAL_QUOTA_KEY, about.getQuotaBytesTotal());
return quotaInfo;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
在getQuotaInfo
方法我捕捉到了異常。我究竟做錯了什麼?