我一直在使用Google Drive API for Android超過50個小時,現在還沒有靠近一英寸。根據我的理解,有1001種訪問Google驅動器的方式(Google Docs API,REST & Google Drive SDK v2)。我正在使用Google Drive SDK v2。我想要訪問Google雲端硬盤來上傳jpeg文件。平臺,Android 2.2+。谷歌驅動器SDK 2.0拋出錯誤400錯誤的請求
我已經試過什麼:
使用最近發佈的SDK: http://code.google.com/p/google-api-java-client/wiki/APIs#Drive_API
我看過的谷歌I/O sesssion,但最重要的組成部分(如何創建使用客戶端ID &客戶端密鑰的驅動器對象被遺漏: https://developers.google.com/events/io/sessions/gooio2012/705/
我創建了多個k在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"
}
要清楚,您是否嘗試通過基於Web的應用程序而不是通過您自己的服務器將文件從Android添加到Google Drive? –
@MorrisonChang可以,直接從Android安裝到Google Drive。我正在使用以下鏈接中的庫。只有該Web應用程序嗎?從我的理解谷歌I/O視頻談論這個API,不是嗎?從Android上傳文件的推薦方式是什麼? http://code.google.com/p/google-api-java-client/wiki/APIs#Drive_API – tofi9