3

我正在创建一个在 Android 上使用 Google Drive 的应用程序。

我在互联网上搜索了一些示例,但它们都是相同的。我收到以下错误:

Object Drive.Builder does not have method setJsonHttpRequestInitializer.

我已经用谷歌驱动 API V1 和 V2 进行了测试,但没有运气。

问题出在哪里?

资源:

private Drive getDriveService(String accountName) {

   HttpTransport ht = AndroidHttp.newCompatibleTransport();                     
   JacksonFactory jf = new JacksonFactory();          
   Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accountName);           

        Drive.Builder b = new Drive.Builder(ht, jf, null);
        b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {

            @Override
            public void initialize(JsonHttpRequest request) throws IOException {
                DriveRequest driveRequest = (DriveRequest) request;
                driveRequest.setPrettyPrint(true);
                driveRequest.setOauthToken(accountName);
                driveRequest.setKey(API_KEY);
            }
        });
        return b.build();
} 

如果使用 google-api-java-client-1.12.0-beta 发现 setJsonHttpRequestInitializer 方法未定义

下载并导入 google-api-java-client-1.11.0-beta

但现在得到:使用客户端 ID 安装应用程序:客户端 ID。

com.google.api.client.http.HttpResponseException: 403 Forbidden
 {
  "error": {
   "errors": [
    {
     "domain": "usageLimits",
     "reason": "accessNotConfigured",
     "message": "Access Not Configured"
    }
   ],
   "code": 403,
   "message": "Access Not Configured"
  }
 }

使用简单 API 访问:API 密钥

Exceptioncom.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"
}

以下是项目来源:

http://www.sendspace.com/file/an6xxy

出了什么问题,也许是证书指纹(SHA1)?

尝试谷歌的 calendar-android-sample。

看起来我在 google api 控制台中注册应用程序有问题。

在哪里放置我在样本中获得的客户 ID?

在 android 中以调试模式运行示例后,得到“访问未配置”。

4

2 回答 2

2

在 Android 上,最佳做法是为 OAuth 2.0 使用 Google Play 服务。从库的 1.12.0-beta 版开始,您应该使用GoogleAccountCredential来完成此操作。

请使用我编写的示例calendar-android-sample作为 Android 上 OAuth 2.0 最佳实践的指南。请仔细阅读说明。请注意,您必须首先在Google API 控制台中注册您的应用程序才能使其正常工作。示例中的代码片段:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  // Google Accounts
  credential = GoogleAccountCredential.usingOAuth2(this, CalendarScopes.CALENDAR);
  SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
  credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
  // Calendar client
  client = new com.google.api.services.calendar.Calendar.Builder(
      transport, jsonFactory, credential).setApplicationName("Google-CalendarAndroidSample/1.0")
      .build();
}
于 2012-11-19T17:01:46.527 回答
1

您可能使用的是较旧版本的 Java 库。

无论如何,您可以将GoogleCredential对象的实例用作HttpRequestInitializer,如以下代码所示:

private Drive getDriveService(String token) {
  Credential credential = new GoogleCredential().setAccessToken(token);
  Drive.Builder b = new Drive.Builder(
      AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential)
      .setHttpRequestInitializer(credential);
  return b.build();
}
于 2012-11-16T00:35:15.533 回答