私はAndroid用のGoogleドライブAPIと50時間以上戦ってきましたが、1インチも近づいていません。私の理解では、Googleドライブ(Google Docs API、REST、Google Drive SDK v2)にアクセスする方法は1001あります。Google DriveSDKv2を使用しています。Googleドライブにアクセスしてjpegファイルをアップロードしたい。プラットフォーム、Android2.2以降。
私が試したこと:
最近リリースされたSDKの使用: http ://code.google.com/p/google-api-java-client/wiki/APIs#Drive_API
Google I / Oセッションを見てきましたが、最も重要な部分(クライアントIDとクライアントシークレットを使用してドライブオブジェクトを作成する方法)が省略されました: https ://developers.google.com/events/io/sessions / gooio2012 / 705 /
https://code.google.com/apis/consoleで複数のキーを作成しました。私が最後に作成した(そしてテストした)ものは、「別のクライアントIDを作成...」->「インストールされたアプリケーション」->「Android」を使用して作成されました。〜/ .android/debug.keystoreのキーを使用しました。
また、(Android / iOSの代わりに)インストールされたアプリのキーを作成しようとしましたが、これによりクライアントIDとクライアントシークレットが得られます。Driveオブジェクトがクライアントシークレットを受け入れていないようです。
コードに「1234567890-abcdefghij123klmnop.apps.googleusercontent.com」と表示されている場合、「APIキー」と「クライアントID」の両方を使用しようとしましたが、どちらも同じエラーが発生しました。
私のコード:
Account account = AccountManager.get(context).getAccountsByType(
"com.google")[0];
String token;
try {
token = GoogleAuthUtil.getToken(context, account.name, "oauth2:"
+ DriveScopes.DRIVE_FILE);
} catch (UserRecoverableAuthException e) {
context.startActivityForResult(e.getIntent(), ASK_PERMISSION);
return;
} catch (IOException e) {
return;
} catch (GoogleAuthException e) {
return;
}
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
final String tokenCopy = token;
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
public void initialize(JsonHttpRequest request) throws IOException {
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setPrettyPrint(true);
driveRequest
.setKey("1234567890-abcdefghij123klmnop.apps.googleusercontent.com");
driveRequest.setOauthToken(tokenCopy);
}
});
final Drive drive = b.build();
FileList files;
try {
files = drive.files().list().setQ("mimeType=text/plain").execute();
} catch (IOException e) {
e.printStackTrace(); // throws HTTP 400
}
私が得ているエラーは次のとおりです。
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"location" : "q",
"locationType" : "parameter",
"message" : "Invalid Value",
"reason" : "invalid"
} ],
"message" : "Invalid Value"
}