Java 从谷歌电子表格中读取数据

Java 从谷歌电子表格中读取数据,java,google-sheets,google-spreadsheet-api,Java,Google Sheets,Google Spreadsheet Api,我想从谷歌电子表格中读取数据。以下是我迄今为止尝试过的东西: 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 MySpreadsheetIn

我想从谷歌电子表格中读取数据。以下是我迄今为止尝试过的东西:

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());
    }
  }
}
import com.google.gdata.client.spreadsheet.*;
导入com.google.gdata.data.spreadsheet.*;
导入com.google.gdata.util.*;
导入java.io.IOException;
导入java.net。*;
导入java.util.*;
公共类MySpreadsheetIntegration1{
公共静态void main(字符串[]args)
引发AuthenticationException、MalformedURLException、IOException、ServiceException{
电子表格服务=
新电子表格服务(“电子表格样本”);
//TODO:为特定用户授权服务对象(请参阅其他部分)
//定义要请求的URL。这永远不会更改。
URL电子表格\u提要\u URL=新URL(
"https://docs.google.com/spreadsheets/d/1jeo8zOv34wVmIX3rL48GksZgj0ixgHx_cwDCdZyQKCg/edit#gid=0");
//向API发出请求并获取所有电子表格。
SpreadsheetFeed=service.getFeed(电子表格\u feed\u URL,SpreadsheetFeed.class);
列表电子表格=feed.getEntries();
//遍历返回的所有电子表格
用于(电子表格输入电子表格:电子表格){
//将此电子表格的标题打印到屏幕上
System.out.println(电子表格.getTitle().getPlainText());
}
}
}
当我运行此代码时,我得到连接超时错误。我觉得这是认证问题

有人能帮我解决这个问题吗

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

所以你需要实际创建一个OAuth会话来通过谷歌验证你自己

下面是我用于当前项目的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());
}

要从Google或官方网站了解更多关于OAuth2.0的信息,请访问:如果合并了这些值,如何读取数据?如下图所示。
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
service.setOAuth2Credentials(getCredentials());