これが認証トークンをリクエストするための私のコードです...
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を取得する方法は次のとおりです。
- 私はGoogleAPIコンソールまたはそれが呼ばれるものに行きました。
- 新製品を作りました。
- Drive SDKを有効にしてセットアップしました(Webアプリを作成していないので、自分が所有するWebサイトにURLを指定しただけです)。
- [APIアクセス]で[別のクライアントIDを作成...]をクリックし、インストールされているAndroidアプリ用に設定しました。(指紋が正しくない可能性がありますか?それがエラーの原因になりますか?)
- インストールされているアプリケーションのクライアントIDの下にリストされているクライアントIDをコピーしました(ドライブ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であり、メッセージはAccessNotFiguredです。