Read data from Google Spreadsheets

痴心易碎 提交于 2019-12-24 03:37:17

问题


Am looking to read data from Google Spreadsheets. Here are the things I tried so far:

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

When I run this code I get connection time out error. I feel it's some authentication problem.

Can someone please help me on how to achieve this?

Here is the link to the spreadsheet I have created for test purposes. Also lot of code available in Google but none of them works so far.


回答1:


So you need to actually create an OAuth session to authenticate yourself with google.

Here is the OAuth code I use for my current project!

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

Then you simply call it like so

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


来源:https://stackoverflow.com/questions/32860225/read-data-from-google-spreadsheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!