如何使用 Google Docs C# API 访问公共电子表格?

发布于 2024-09-13 05:01:31 字数 1142 浏览 2 评论 0原文

我使用 Google Docs .NET API 编写了一个 C# 程序,在给定用户名、密码、电子表格名称和工作表名称的情况下将 Google 工作表读入 DataTable。这一切都工作正常,但编程模型似乎围绕着为电子表格服务提供一组凭据,然后削减生成的提要以获得特定的电子表格/工作表,即

        SpreadsheetsService service = new SpreadsheetsService("Application-Name");
        service.setUserCredentials(userName, password);

        SpreadsheetQuery spreadsheetQuery = new SpreadsheetQuery();
        SpreadsheetFeed spreadsheetFeed = service.Query(spreadsheetQuery);

        SpreadsheetEntry spreadsheetEntry = (SpreadsheetEntry)(from entries in spreadsheetFeed.Entries
                                                               where entries.Title.Text == spreadsheetName
                                                               select entries).SingleOrDefault();

现在我有兴趣扩展我的程序的功能从公共 Google 电子表格中读取。也就是说,给定公共 Google 电子表格的网址(例如“https:// Spreadsheets.google.com/ccc?key=BUNCH_OF_LETTERS_HERE&hl=en"),我想获取与该文档对应的 SpreadsheetEntry 对象。

到目前为止,我一直在使用的方法显然似乎无法扩展以允许这样做,所以我想知道是否有人知道通过他们的 API 访问公共 Google 文档的正确方法?

I've written a C# program using the Google Docs .NET API to read a Google worksheet into a DataTable given a user name, password, spreadsheet name, and worksheet name. This all works fine, but the programming model seems to revolve around giving the spreadsheet service a set of credentials, and then paring down the resulting feed to get a specific spreadsheet/worksheet, i.e.

        SpreadsheetsService service = new SpreadsheetsService("Application-Name");
        service.setUserCredentials(userName, password);

        SpreadsheetQuery spreadsheetQuery = new SpreadsheetQuery();
        SpreadsheetFeed spreadsheetFeed = service.Query(spreadsheetQuery);

        SpreadsheetEntry spreadsheetEntry = (SpreadsheetEntry)(from entries in spreadsheetFeed.Entries
                                                               where entries.Title.Text == spreadsheetName
                                                               select entries).SingleOrDefault();

Now I'm interested in extending the functionality of my program to read from public Google spreadsheets. That is, given the URL of a public Google spreadsheet (e.g. "https://spreadsheets.google.com/ccc?key=BUNCH_OF_LETTERS_HERE&hl=en"), I want to get the SpreadsheetEntry object corresponding to that document.

The method I've been using thus far obviously does not seem to extend to allow this, so I was wondering if anyone knows the proper way to access a public Google document through their API?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-09-20 05:01:31

您可以简单地检索公共页面,然后解析行的 HTML 源。

请求标头:

GET https://docs.google.com/spreadsheet/ccc?key=0Au1XehjMeFfYdEE2d0RDSk1FMEMtRjM0MmllUWdoTkE HTTP/1.1
Host: docs.google.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko)     Chrome/15.0.874.120 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3
Cookie: PREF=ID=68126eb9eb12adef:FF=0:TM=1331371284:LM=1331371284:S=Bkm9mYX8pYy9a4h9

电子表格网格 - 列标签:

<table class="waffle" cellspacing="0" cellpadding="0">

<thead><tr>
<th class="row-header freezebar-top-left"></th>
<th style="width:120px" class="column-headers-background">A</th>
<th style="width:120px" class="column-headers-background">B</th>
<th style="width:120px" class="column-headers-background">C</th>
</tr></thead>

<tbody>

第一行作为用户定义的列名称:

<tr style='height:16px;'>
<th style="height: 16px;" class="row-headers-background"><div class="row-header-wrapper" style="height: 16px;">1</div></th>

<td class="g-0-GjugEgs0" dir="auto-ltr">username</td>
<td class="g-0-GjugEgs0" dir="auto-ltr">create</td>
<td class="g-0-GjugEgs0" dir="auto-ltr">expire</td>
</tr>

其余行作为用户数据:
行号:

<tr style='height:16px;'>
<th style="height: 16px;" class="row-headers-background"><div class="row-header-wrapper" style="height: 16px;">2</div></th>

行数据单元格:

<td class="g-0-GjugEgs2">3600001</td>
<td class="g-0-GjugEgs2">2012</td>
<td class="g-0-GjugEgs2">2013</td>
</tr>

您可以使用 Html Agility Pack 或您自己的解析器。

另一种选择是将公共电子表格下载为 CSV 或文本格式的

文件 文件下载请求标头:

GET https://docs.google.com/spreadsheet/fm?id=tA6wDCJME0C-F342ieQghNA.PREF_08255578241116458508.3736592253424693644&fmcmd=23&gid=0 HTTP/1.1
Host: docs.google.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: https://docs.google.com/spreadsheet/ccc?key=0Au1XehjMeFfYdEE2d0RDSk1FMEMtRjM0MmllUWdoTkE
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3
Cookie: PREF=ID=68126eb9eb12adef:FF=0:TM=1331371284:LM=1331371284:S=Bkm9mYX8pYy9a4h9; lbcs=0

请注意 id 参数。你必须从 html 源中提取它,如下所示:

 ...
 var mergedConfig = {"formToken":"Kg2uOS1UniIe0yFks5zcDZDsGQ=","formStatus":false,"id":"tA6wDCJME0C-F342ieQghNA.PREF_08255578241116458508.3736592253424693644",
 ...

我使用 提琴手 捕获包括 SSL 加密消息在内的通信。

You could simply retrieve the public page and then parse HTML source for the rows.

Request headers:

GET https://docs.google.com/spreadsheet/ccc?key=0Au1XehjMeFfYdEE2d0RDSk1FMEMtRjM0MmllUWdoTkE HTTP/1.1
Host: docs.google.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko)     Chrome/15.0.874.120 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3
Cookie: PREF=ID=68126eb9eb12adef:FF=0:TM=1331371284:LM=1331371284:S=Bkm9mYX8pYy9a4h9

Spreadsheet grid - Column labels:

<table class="waffle" cellspacing="0" cellpadding="0">

<thead><tr>
<th class="row-header freezebar-top-left"></th>
<th style="width:120px" class="column-headers-background">A</th>
<th style="width:120px" class="column-headers-background">B</th>
<th style="width:120px" class="column-headers-background">C</th>
</tr></thead>

<tbody>

First row as user defined column names:

<tr style='height:16px;'>
<th style="height: 16px;" class="row-headers-background"><div class="row-header-wrapper" style="height: 16px;">1</div></th>

<td class="g-0-GjugEgs0" dir="auto-ltr">username</td>
<td class="g-0-GjugEgs0" dir="auto-ltr">create</td>
<td class="g-0-GjugEgs0" dir="auto-ltr">expire</td>
</tr>

Rest of the rows as user data:
Row number:

<tr style='height:16px;'>
<th style="height: 16px;" class="row-headers-background"><div class="row-header-wrapper" style="height: 16px;">2</div></th>

Row data cells:

<td class="g-0-GjugEgs2">3600001</td>
<td class="g-0-GjugEgs2">2012</td>
<td class="g-0-GjugEgs2">2013</td>
</tr>

You could use Html Agility Pack or your own parser.

Another option is downloading the public spreadsheet as file in CSV or text format

File download request headers:

GET https://docs.google.com/spreadsheet/fm?id=tA6wDCJME0C-F342ieQghNA.PREF_08255578241116458508.3736592253424693644&fmcmd=23&gid=0 HTTP/1.1
Host: docs.google.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: https://docs.google.com/spreadsheet/ccc?key=0Au1XehjMeFfYdEE2d0RDSk1FMEMtRjM0MmllUWdoTkE
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3
Cookie: PREF=ID=68126eb9eb12adef:FF=0:TM=1331371284:LM=1331371284:S=Bkm9mYX8pYy9a4h9; lbcs=0

Notice the id parameter. You have to extract it from the html source like below:

 ...
 var mergedConfig = {"formToken":"Kg2uOS1UniIe0yFks5zcDZDsGQ=","formStatus":false,"id":"tA6wDCJME0C-F342ieQghNA.PREF_08255578241116458508.3736592253424693644",
 ...

I used Fiddler to capture communication including SSL encrypted messages.

-黛色若梦 2024-09-20 05:01:31

我试图阅读带有多个选项卡的电子表格,当我实现这些其他答案(即向页面发出直接网络请求)时返回的 html 和 javascript 几乎无法读取。经过进一步挖掘,我发现如果您确实想获取公共 Google 电子表格的值而不处理任何 OAuth 内容,您首先需要转到 https://console.developers.google.com/apis,创建一个项目,转到仪表板,然后转到凭据,单击“创建凭据”并设置创建一个新的服务帐户密钥(选择 json 并保存 json 文件。然后您可以使用它来获取任何公共电子表格):

using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;

GoogleCredential credential;
string spreadSheetId = "k3k5j5nh4h43j5ktb4gf5h5l5"; //https://docs.google.com/spreadsheets/d/k3k5j5nh4h43j5ktb4gf5h5l5/
using(var stream = new FileStream('YOUR_JSON_FILE_PATH', FileMode.Open, FileAccess.Read)) 
{
    credential = GoogleCredential.FromStream(stream).CreateScoped(SheetsService.Scope.SpreadsheetsReadonly);

    var sheetsService = new SheetsService(new BaseClientService.Initializer() 
    {
        HttpClientInitializer = credential,
        ApplicationName = "You application name",
    });

    var range = "A:B";

    var request = sheetsService.Spreadsheets.Values.Get(spreadSheetId, range);

    ValueRange response = request.Execute();
}

I was trying to read a spreadsheet with multiple tabs and the html and javascript that came back when I implemented these other answers(i.e. making a direct web request to the page) was almost unreadable. After further digging, I found that if you really want to get the values of a public Google spreadsheet without having to deal with any OAuth stuff, you first need to go to https://console.developers.google.com/apis, create a project, go to the dashboard, and go to credentials, click 'create credentials' and set up a new service account key(choose json and save the json file. Then you can use it to get any public spreadsheet):

using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;

GoogleCredential credential;
string spreadSheetId = "k3k5j5nh4h43j5ktb4gf5h5l5"; //https://docs.google.com/spreadsheets/d/k3k5j5nh4h43j5ktb4gf5h5l5/
using(var stream = new FileStream('YOUR_JSON_FILE_PATH', FileMode.Open, FileAccess.Read)) 
{
    credential = GoogleCredential.FromStream(stream).CreateScoped(SheetsService.Scope.SpreadsheetsReadonly);

    var sheetsService = new SheetsService(new BaseClientService.Initializer() 
    {
        HttpClientInitializer = credential,
        ApplicationName = "You application name",
    });

    var range = "A:B";

    var request = sheetsService.Spreadsheets.Values.Get(spreadSheetId, range);

    ValueRange response = request.Execute();
}
ら栖息 2024-09-20 05:01:31

Google 文档列表 API 不支持似乎为此设置(参见ahab的回答)。

The Google Docs List API doesn't seem setup for that (see ahab's answer).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文