好吧,该死的 Java API 显然不支持访问所有文件所需的https://www.googleapis.com/auth/drive范围。我试过这个:
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(new Scope("https://www.googleapis.com/auth/drive"))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
结果是“来自 Google API 的未知错误”和日志中的异常:
06-30 14:11:48.803 3023-29855/? E/ClientConnectionOperation﹕ Handling authorization failure
com.google.android.gms.drive.auth.c: Authorization failed: Unsupported scope: https://www.googleapis.com/auth/drive
at com.google.android.gms.drive.auth.g.a(SourceFile:77)
at com.google.android.gms.drive.api.g.<init>(SourceFile:226)
at com.google.android.gms.drive.api.a.k.a(SourceFile:46)
at com.google.android.gms.common.service.g.run(SourceFile:178)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)`
然后我尝试了另一个范围:https://www.googleapis.com/auth/drive.readonly没有以不同的方式工作:
06-30 14:12:25.082 3023-29981/? E/ClientConnectionOperation﹕ Handling authorization failure
com.google.android.gms.drive.auth.c: Authorization failed: No valid Drive authorization scope provided.
at com.google.android.gms.drive.auth.g.a(SourceFile:87)
at com.google.android.gms.drive.api.g.<init>(SourceFile:226)
at com.google.android.gms.drive.api.a.k.a(SourceFile:46)
at com.google.android.gms.common.service.g.run(SourceFile:178)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
在 Google APIs 7.5.0 上试过这个,在这个延迟的 API 上浪费了两个小时。我相信 Java API 不能正确处理这个问题,我们需要用 JSON API 来做这件事。
编辑:我发现这个关于如何使用另一个 Java API 访问 Google Drive 的非常好的教程:https ://developers.google.com/drive/web/quickstart/java
不幸的是,它不适用于 Android:
Caused by: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:190)
at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:139)
at java.security.KeyStore.getInstance(KeyStore.java:116)
在 com.google.api.client.util.SecurityUtils.getJavaKeyStore(SecurityUtils.java:53)
爱丽丝梦游仙境有人吗?