一切正常播放服務:身份驗證谷歌硬碟SDK
String token = GoogleAuthUtil.getToken(mActivity, mEmail, "oauth2:" + DriveScopes.Drive);
與令牌我現在可以創建我的驅動實例,並訪問谷歌驅動器。但我意識到,這隻支持Android 2.2及以上。我需要支持android 2.1。
我曾嘗試使用此代碼來獲取令牌:
AccountManager am = AccountManager.get(this);
Bundle options = new Bundle();
am.getAuthToken (
account,
"oauth2:" + DriveScopes.Drive,
options,
this,
new OnTokenAcquired(this),
null);
使用此代碼時,我得到一個令牌,但使用它時,創建我的硬盤比如我不會得到訪問谷歌驅動器。舉例而言,當執行此代碼:
drive.files().list().setQ(trashed=false and title="'someTitle'").execute();
我會得到這個錯誤:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
這是如何創建我的驅動器實例:
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
public void initialize(JsonHttpRequest request) throws IOException {
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setPrettyPrint(true);
driveRequest.setKey(CLIENT_ID);
driveRequest.setOauthToken(token);
driveRequest.setEnableGZipContent(true);
}
});
drive = b.build();
您使用的是JacksonFactory的庫/ jar文件夾?我得到了NoClassDef發現錯誤 – drulabs
@KKD我用[google插件](https://developers.google.com/eclipse/docs/getting_started)添加了所需的所有jar文件。根據Eclipse com.google.api.client.json.jackson2.JacksonFactory' – iixi