这是我请求身份验证令牌的代码...
AccountManager am = AccountManager.get(this);
Bundle options = new Bundle();
am.getAuthToken(
(am.getAccounts())[0], // My test device only has one account. I'll add a picker before releasing this app.
"oauth2:" + DriveScopes.DRIVE,
options,
true, // I've tried both true and false... doesn't seem to change anything?
new OnTokenAcquired(),
null); // I used to have a handler, but it absolutely never got called.
这是处理令牌的代码:
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
@Override
public void run(AccountManagerFuture<Bundle> result) {
String token = result.getResult().getString(AccountManager.KEY_AUTHTOKEN);
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest request) throws IOException {
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setPrettyPrint(true);
driveRequest.setKey("xxxxxxxxxxxx.apps.googleusercontent.com"); // I replaced the number with x's. I'll explain where I got the number from below.
driveRequest.setOauthToken(token);
}
});
final Drive drive = b.build();
final com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain");
java.io.File fileContent = new java.io.File("document.txt");
final FileContent mediaContent = new FileContent("text/plain", fileContent);
new Thread(new Runnable() {
public void run() {
com.google.api.services.drive.model.File file;
try {
file = drive.files().insert(body, mediaContent).execute();
Log.i("Hi", "File ID: " + file.getId());
} catch (IOException e) {
Log.i("Hi", "The upload/insert was caught, which suggests it wasn't successful...");
e.printStackTrace();
AccountManager am = AccountManager.get(activity);
am.invalidateAuthToken(am.getAccounts()[0].type, null);
Bundle options = new Bundle();
am.getAuthToken(
(am.getAccounts())[0],
"oauth2:" + DriveScopes.DRIVE,
options,
true,
new OnTokenAcquired(),
null);
}
}
}).start();
Intent launch = (Intent)result.getResult().get(AccountManager.KEY_INTENT);
if (launch != null) {
Log.i("Hi", "Something came back as a KEY_INTENT");
startActivityForResult(launch, 3025);
return;
} else {
Log.i("Hi", "I checked, but there was nothing for KEY_INTENT.");
}
}
}
我已经实现了 onActivityResult 并将其设置为记录 requestCode 和 resultCode 如果它曾经被调用过,但它从来没有被调用过。如果它被调用,它只会触发另一个令牌请求,如果 requestCode 是 3025 并且 resultCode 是 RESULT_OK。
以下是我获得客户 ID 的方式:
- 我去了 Google APIs 控制台或其他任何名称。
- 我做了一个新产品。
- 我启用并设置了 Drive SDK(有点……我不是在制作网络应用程序,所以我只是将 URL 指向我拥有的网站。)
- 在“API 访问”下,我单击“创建另一个客户端 ID...”并为已安装的 Android 应用程序进行设置。(指纹可能不太对?那会导致错误吗?)
- 我复制了已安装应用程序的客户端 ID 下列出的客户端 ID(不是 Drive SDK 的客户端 ID)。
事实上,这是我从日志中得到的:
I checked, but there was nothing for KEY_INTENT // My log message.
The upload/insert was caught, which suggests it didn't work... // Another one of my log messages.
com.google.api.client.http.HttpResponseException: 400 Bad Request
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "keyInvalid",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
接下来是更普通的堆栈跟踪......
编辑 最近它停止给我400,而是开始给我403 Forbidden,仍然使用usageLimits域,原因现在是accessNotConfigured,消息是Access Not Figd。