【问题标题】:The method setJsonHttpRequestInitializer is undefined for the type Drive.Builder Android未为 Drive.Builder Android 类型定义方法 setJsonHttpRequestInitializer
【发布时间】:2012-11-15 13:37:38
【问题描述】:

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

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

Object Drive.Builder does not have method setJsonHttpRequestInitializer.

我已经用 Google drive 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 中以调试模式运行示例后,出现“未配置访问权限”。

【问题讨论】:

    标签: android google-drive-api google-api-java-client


    【解决方案1】:

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

    请使用我编写的示例 calendar-android-sample 作为 Android 上 OAuth 2.0 最佳实践的指南。请仔细阅读说明。请注意,您必须首先在 Google APIs Console 中注册您的应用程序才能使用。示例中的代码 sn-p:

    @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();
    }
    

    【讨论】:

    • 尝试为日历示例创建客户端 ID,但得到:发生意外错误。我们正在调查它。这是在谷歌上临时的吗?
    • 发现是因为包名太长了。
    • 尝试日历示例,在哪里设置我在 google api 控制台中制作的客户端 ID?
    • @user1826780 非常感谢。我对此很生气,你的评论拯救了我的一天!
    【解决方案2】:

    您可能使用的是旧版本的 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();
    }
    

    【讨论】:

    • 是的,这有帮助。但是现在没有配置访问权限。我在 Google Drive 的 google api 控制台中创建了 API KEY 并在 Manifets 中编写了它,但没有运气。 Exceptioncom.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "usageLimits", "message" : "Access Not Configured", "reason" : "accessNotConfigured" } ], "message" : "访问未配置" }
    • Drive API 不使用 API 密钥进行授权,而是您需要一个授权令牌,如 developers.google.com/android/google-play-services/… 中所述
    • 添加了更多信息,有人可以帮忙吗?
    • 查看 Drive 的新 Android 快速入门:developers.google.com/drive/quickstart-android
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2014-06-28
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多