身份验证谷歌硬碟SDK
问题描述:
一切正常播放服务:身份验证谷歌硬碟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();
答
好吧,我已经解决了它。问题实际上是我使用了错误的客户端ID。在使用Google Play服务时,我使用客户端ID来查找Google API控制台中已安装的应用程序。当使用AccountManager这个ID不起作用,而不得不使用API密钥进行简单API访问。
编辑
发现,这是不完全正确的。看起来不同的设备使用的客户端ID是不同的。
答
这似乎是正确的,但问题AccountManager OAuth2支持取决于平台版本(Google Services库等)。它可能适用于某些设备,可能不适用于其他库略有不同的其他设备。确保它在2.1上始终如一地工作的唯一方法是使用WebView来获取令牌。如果AccountManager在某些设备上损坏或出现故障,您无法做任何事情。
您使用的是JacksonFactory的库/ jar文件夹?我得到了NoClassDef发现错误 – drulabs
@KKD我用[google插件](https://developers.google.com/eclipse/docs/getting_started)添加了所需的所有jar文件。根据Eclipse com.google.api.client.json.jackson2.JacksonFactory' – iixi