java - 从 Google 电子表格中读取数据

标签 java google-sheets google-sheets-api

我希望从 Google 电子表格中读取数据。以下是我到目前为止尝试过的事情:

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.net.*;
import java.util.*;

public class MySpreadsheetIntegration1 {
  public static void main(String[] args)
      throws AuthenticationException, MalformedURLException, IOException, ServiceException {

    SpreadsheetService service =
        new SpreadsheetService("SpreadSheetSample");

    // TODO: Authorize the service object for a specific user (see other sections)

    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(
        "https://docs.google.com/spreadsheets/d/1jeo8zOv34wVmIX3rL48GksZgj0ixgHx_cwDCdZyQKCg/edit#gid=0");

    // Make a request to the API and get all spreadsheets.
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    // Iterate through all of the spreadsheets returned
    for (SpreadsheetEntry spreadsheet : spreadsheets) {
      // Print the title of this spreadsheet to the screen
      System.out.println(spreadsheet.getTitle().getPlainText());
    }
  }
}

当我运行这段代码时,出现连接超时错误。我觉得这是一些身份验证问题。

有人可以帮我实现这个目标吗?

Here是我为测试目的创建的电子表格的链接。 谷歌也提供了很多代码,但目前没有一个有效。

最佳答案

因此,您需要实际创建一个 OAuth session 以向 google 验证您自己。

这是我在当前项目中使用的 OAuth 代码!

/**
 * Retrieve OAuth 2.0 credentials.
 *
 * @return OAuth 2.0 Credential instance.
 */
static Credential getCredentials() throws IOException {
    HttpTransport transport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    // Step 1: Authorize -->
    String authorizationUrl =
            new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, REDIRECT_URI, SCOPES).build();

    // Point or redirect your user to the authorizationUrl.
    System.out.println("Go to the following link in your browser:");
    System.out.println(authorizationUrl);

    // Read the authorization code from the standard input stream.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is the authorization code?");
    String code = in.readLine();
    // End of Step 1 <--

    // Step 2: Exchange -->
    GoogleTokenResponse response =
            new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
                    code, REDIRECT_URI).execute();
    // End of Step 2 <--

    authenticated = true;
    // Build a new GoogleCredential instance and return it.
    return new GoogleCredential.Builder().setClientSecrets(CLIENT_ID, CLIENT_SECRET)
            .setJsonFactory(jsonFactory).setTransport(transport).build()
            .setAccessToken(response.getAccessToken()).setRefreshToken(response.getRefreshToken());
}

然后你就这样调用它

SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
service.setOAuth2Credentials(getCredentials());

关于java - 从 Google 电子表格中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32860225/

相关文章:

java - JUnit4:is() 与 is(notNullValue())

javascript - 为什么我的单选按钮响应无法响应我的 Google 电子表格?

javascript - 将 Google 电子表格解析为 Javascript 数组

google-apps-script - 在 Google Apps 脚本中获取历史股票价格

python - Google 表格 + GoogleCredentials = "insufficient authentication scopes"

python - 需要帮助优化 gspread API 调用

java - 如何使用 AbstractProcessor 访问另一个注解中的注解 - 而不是反射

java - getChar 函数如何工作?

java.io.FilePermission 异常 - 从 jar 文件读取?

google-sheets - Google 表格 IMPORTHTML "Could not fetch data"