从Google Spreadsheets中读取数据 [英] Read data from Google Spreadsheets

查看:249
本文介绍了从Google Spreadsheets中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找从Google Spreadsheets中读取数据。以下是我迄今为止所尝试的内容:

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

import java.io.IOException;
导入java.net。*;
import java.util。*;

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

SpreadsheetService service =
新SpreadsheetService(SpreadSheetSample);

// TODO:为特定用户授权服务对象(请参阅其他部分)

//定义要请求的URL。这应该永远不会改变。
URL SPREADSHEET_FEED_URL =新网址(
https://docs.google.com/spreadsheets/d/1jeo8zOv34wVmIX3rL48GksZgj0ixgHx_cwDCdZyQKCg/edit#gid=0);

//向API发出请求并获取所有电子表格。
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,SpreadsheetFeed.class);
列表< SpreadsheetEntry> spreadsheets = feed.getEntries();

//遍历所有电子表格返回
for(SpreadsheetEntry电子表格:spreadsheets){
//将此电子表格的标题打印到屏幕
System。通过out.println(spreadsheet.getTitle()getPlainText());
}
}
}

连接超时错误。我觉得这是一些认证问题。

有人可以帮我解决这个问题吗?



此处是我为测试目的创建的电子表格的链接。
在Google中也有很多可用的代码,但目前为止它们都没有工作。

解决方案

一个OAuth会话来验证你自己与谷歌。



这是我用于当前项目的OAuth代码!

  / ** 
*检索OAuth 2.0凭据。
*
* @return OAuth 2.0凭据实例。
* /
静态Credential getCredentials()抛出IOException {
HttpTransport transport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();

//第1步:授权 - >
String authorizationUrl =
GoogleAuthorizationCodeRequestUrl(CLIENT_ID,REDIRECT_URI,SCOPES).build();

//将用户指向或重定向到authorizationUrl。
System.out.println(转到浏览器中的以下链接:);
System.out.println(authorizationUrl);

//从标准输入流中读取授权码。
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(什么是授权码?);
String code = in.readLine();
//第1步结束< -

//第2步:Exchange - >
GoogleTokenResponse response =
GoogleAuthorizationCodeTokenRequest(transport,jsonFactory,CLIENT_ID,CLIENT_SECRET,
code,REDIRECT_URI).execute();
//第二步结束< -

authenticated = true;
//构建一个新的GoogleCredential实例并将其返回。
返回新的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());


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.

解决方案

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

这篇关于从Google Spreadsheets中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆