使用Google Spreadsheet API在Google驱动器中使用Java创建电子表格

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (109)

我创建了一些工具来填充谷歌电子表格。它工作正常1年,因为今天我错了

Exception in thread "main" com.google.gdata.util.AuthenticationException: Error authenticating (check service name)
at com.google.gdata.client.GoogleAuthTokenFactory.getAuthException(GoogleAuthTokenFactory.java:688)
at com.google.gdata.client.GoogleAuthTokenFactory.getAuthToken(GoogleAuthTokenFactory.java:560)
at com.google.gdata.client.GoogleAuthTokenFactory.setUserCredentials(GoogleAuthTokenFactory.java:397)
at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:364)
at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:319)
at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:303)

这是与gmail连接的代码的一部分:

String USERNAME = "usename@........com"; ->of course I'm using proper username and password
    String PASSWORD = "*******";
    SpreadsheetService service
            = new SpreadsheetService("SandboxCheck");
    service.setUserCredentials(USERNAME, PASSWORD);

我不知道如何连接到Gmail,我试图通过oAuth来做到这一点,但我不知道如何做到这一点。在https://developers.google.com/google-apps/spreadsheets/authorize上的示例中,只有.net的代码。

提问于
用户回答回答于

我终于在这里的帮助下创建了这样的连接。一切都像以前一样工作。步骤你需要做的是:

  1. https://console.developers.google.com上注册
  2. 创建新项目
  3. 在API和身份验证 - >凭据 - >为服务帐户创建新的客户端ID
  4. 当客户端ID生成时您必须生成P12密钥。
  5. 在下面的代码中需要客户端ID,电子邮件地址是地址,您必须共享您的电子表格

以下是工作代码

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
public class OAuthIntegration{
    public static void main(String[] args) throws MalformedURLException, GeneralSecurityException, IOException, ServiceException {
        URL SPREADSHEET_FEED_URL;
        SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");

        File p12 = new File("./key.p12");

        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();
        String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
        final List SCOPES = Arrays.asList(SCOPESArray);
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId("cliend_ID")
                .setServiceAccountScopes(SCOPES)
                .setServiceAccountPrivateKeyFromP12File(p12)
                .build();

        SpreadsheetService service = new SpreadsheetService("Test");

        service.setOAuth2Credentials(credential);
        SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
        List<SpreadsheetEntry> spreadsheets = feed.getEntries();

        if (spreadsheets.size() == 0) {
            System.out.println("No spreadsheets found.");
        }

         SpreadsheetEntry spreadsheet = null;
        for (int i = 0; i < spreadsheets.size(); i++) {
            if (spreadsheets.get(i).getTitle().getPlainText().startsWith("ListOfSandboxes")) {
                spreadsheet = spreadsheets.get(i);
                System.out.println("Name of editing spreadsheet: " + spreadsheets.get(i).getTitle().getPlainText());
                System.out.println("ID of SpreadSheet: " + i);
            }
        }

    }

}

我希望这将有助于我遇到的问题。这是我用过的jar列表:

guava-11.0.2.jar
gdata-spreadsheet-3.0.jar
gdata-maps-2.0.jar
gdata-core-1.0.jar
jackson-core-asl-1.9.11.jar
jackson-core-2.1.3.jar
google-oauth-client-1.20.0.jar
google-http-client-jackson2-1.20.0.jar
google-http-client-jackson-1.20.0.jar
google-http-client-1.20.0.jar
google-api-client-1.20.0.jar
用户回答回答于

Google刚刚停止了对OAuth1.0的支持。OAuth2需要使用。要切换,请先转到Google Developer Console,创建一个项目并设置凭据。然后更新您的代码,类似于以下Java代码:

private void createSpreadSheetService() throws GeneralSecurityException, IOException, ServiceException {
   HttpTransport httpTransport = new NetHttpTransport();
   JacksonFactory jsonFactory = new JacksonFactory();
   String [] SCOPESArray= {"https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds"};
   final List SCOPES = Arrays.asList(SCOPESArray);
   GoogleCredential credential = new GoogleCredential.Builder()
     .setTransport(httpTransport)
     .setJsonFactory(jsonFactory)
     .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
     .setServiceAccountScopes(SCOPES)
     .setServiceAccountPrivateKeyFromP12File(SERVICE_ACCOUNT_PKCS12_FILE)
     .build();

   SPREADSHEETSERVICE = new SpreadsheetService("data");
   SPREADSHEETSERVICE.setOAuth2Credentials(credential);

}

您需要两个库:google-api-client和google-http-client-jackson。如果您使用Maven,包括pom.xml中的以下依赖项。

 <dependency>
  <groupId>com.google.api-client</groupId>
  <artifactId>google-api-client</artifactId>
  <version>1.19.0</version>
  <type>jar</type>
 </dependency>
 <dependency>
  <groupId>com.google.http-client</groupId>
  <artifactId>google-http-client-jackson</artifactId>
  <version>1.19.0</version>
  <type>jar</type>
 </dependency>

扫码关注云+社区

领取腾讯云代金券