这是我请求身份验证令牌的代码......
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时触发另一个令牌请求。
以下是我的clientID:
实际上,这是我从日志中获得的内容:
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 Figured。
答案 0 :(得分:2)
您将从API控制台获取的客户端ID传递给driveRequest.setKey
,但该方法用于设置API密钥而不是客户端ID。这就是为什么您的应用程序没有从API控制台正确绑定到项目的原因,并且您的应用程序不允许使用API。
获得该客户端ID标识的项目的有效OAuth令牌后,您可以通过调用driveRequest.setOauthToken(String)
进行设置。
有关Android上OAuth2的详细信息,请检查http://developer.android.com/training/id-auth/authenticate.html
答案 1 :(得分:0)
问题似乎是在API控制台中我没有在服务下启用 DRIVE SDK 和 DRIVE API 。它们是分开的,一个不会自动转向另一个。