2012-10-16 141 views
2

一切正常播放服务:身份验证谷歌硬碟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(); 
+0

您使用的是JacksonFactory的库/ jar文件夹?我得到了NoClassDef发现错误 – drulabs

+1

@KKD我用[google插件](https://developers.google.com/eclipse/docs/getting_started)添加了所需的所有jar文件。根据Eclipse com.google.api.client.json.jackson2.JacksonFactory' – iixi

回答

0

好吧,我已经解决了它。问题实际上是我使用了错误的客户端ID。在使用Google Play服务时,我使用客户端ID来查找Google API控制台中已安装的应用程序。当使用AccountManager这个ID不起作用,而不得不使用API​​密钥进行简单API访问。

编辑

发现,这是不完全正确的。看起来不同的设备使用的客户端ID是不同的。

0

这似乎是正确的,但问题AccountManager OAuth2支持取决于平台版本(Google Services库等)。它可能适用于某些设备,可能不适用于其他库略有不同的其他设备。确保它在2.1上始终如一地工作的唯一方法是使用WebView来获取令牌。如果AccountManager在某些设备上损坏或出现故障,您无法做任何事情。

+0

,这是JacksonFactory的路径我是否需要在Google Drive SDK中使用OAuth2,还是可以使用另一种与AccountManager一起使用的身份验证? – iixi

+0

我*事*它只支持OAuth2,但在参考等查询它。在任何情况下ClientLogin已被弃用,不提供罚款控制,我不认为AccountManager支持OAuth 1.0,所以真的没有其他可行的选择。 –

+0

所以我解决了我的问题。我添加了一个答案。即使我的问题解决了,我也有点担心你所说的话。当身份验证不起作用时,不要为某些用户问题。 – iixi