带有服务帐户的Google电子表格

时间:2015-07-08 08:55:35

标签: java google-api google-spreadsheet-api

我想使用de java Google电子表格API访问Google云端硬盘上的电子表格。在过去,我使用客户端登录方法登录,但现在不推荐使用此方法。 现在我尝试使用服务帐户进行身份验证而不与用户交互,因为该工具在后台运行我需要自动化。但我无法让它发挥作用。这些我的代码现在。

    List<String> SCOPES_ARRAY = Arrays.asList(
            "https://www.googleapis.com/auth/drive.file",
            "https://www.googleapis.com/auth/userinfo.email",
            "https://www.googleapis.com/auth/userinfo.profile",
            "https://docs.google.com/feeds",
            "https://spreadsheets.google.com/feeds");
    credential = new GoogleCredential.Builder()
                .setTransport(transport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(
                        "xxxxxx@developer.gserviceaccount.com")
                .setServiceAccountScopes(SCOPES_ARRAY)
                .setServiceAccountPrivateKeyFromP12File(p12)
                .setServiceAccountUser("me@gmail.com")
                .build();
    service = new SpreadsheetService("MonitorV3");
    service.setOAuth2Credentials(credential);

    SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    worksheetFeed = service.getFeed(spreadsheet.getWorksheetFeedUrl(),
                    WorksheetFeed.class);

但是我在执行最后一行时遇到了nullpointerexception。刷新令牌出了问题。是否有人看到我做错了什么?enter image description here

我制作了一个独立的简单应用程序,现在我得到了详细的错误:

Exception in thread "main" com.google.gdata.util.AuthenticationException: Failed to refresh access token: 401 Unauthorized
at com.google.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:260)
at com.google.gdata.client.GoogleAuthTokenFactory.handleSessionExpiredException(GoogleAuthTokenFactory.java:702)
at com.google.gdata.client.GoogleService.handleSessionExpiredException(GoogleService.java:738)
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:649)
at com.google.gdata.client.Service.getFeed(Service.java:1017)
at JavaApplication20.printDocuments(JavaApplication20.java:50)
at main.main(main.java:35)

2 个答案:

答案 0 :(得分:3)

我使用以下代码解决了这个问题:

credential = new GoogleCredential.Builder()
            .setTransport(transport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId("618982716759-9ele96d95b7gqar6tn2ofa7jrjrudlol@developer.gserviceaccount.com")
            .setServiceAccountPrivateKeyFromP12File(p12)
            .setServiceAccountScopes(SCOPES_ARRAY).build();
            credential.refreshToken();

所以没有.setServiceAccountUser(&#34; me@gmail.com")。现在它没有任何错误地工作。

答案 1 :(得分:0)

使用Google服务帐户访问Google表格的另一种方法是:

InputStream inputStream = getAssets().open("service_account_key.json"); // put your service account's key.json file in asset folder.

GoogleCredential googleCredential = GoogleCredential.fromStream(inputStream)
                  .createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS_READONLY));

这里要注意的重要事项:

  • 您应该编写代码块以通过线程或异步调用访问Google表格。
  • 如果在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork上遇到任何类似android.os.NetworkOnMainThreadException的异常,则请在进行异步调用之前包括以下几行:

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy);

请参考以下文档以获取详细说明:

https://developers.google.com/sheets/quickstart/android?hl=es-419#step_5_setup_the_sample https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests

Android开发愉快。