Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Google Docs C#API访问公共电子表格?_C#_Google Docs Api_Google Docs - Fatal编程技术网

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

如何使用Google Docs C#API访问公共电子表格?,c#,google-docs-api,google-docs,C#,Google Docs Api,Google Docs,我已经编写了一个C#程序,使用Google Docs.NET API将Google工作表读取到给定用户名、密码、电子表格名称和工作表名称的数据表中。这一切都很好,但编程模型似乎围绕着为电子表格服务提供一组凭证,然后削减结果提要以获得特定的电子表格/工作表,即 SpreadsheetsService service = new SpreadsheetsService("Application-Name"); service.setUserCredentials(us

我已经编写了一个C#程序,使用Google Docs.NET API将Google工作表读取到给定用户名、密码、电子表格名称和工作表名称的数据表中。这一切都很好,但编程模型似乎围绕着为电子表格服务提供一组凭证,然后削减结果提要以获得特定的电子表格/工作表,即

        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电子表格(例如“”)的URL,我想获得与该文档对应的SpreadsheetEntry对象


到目前为止,我使用的方法显然没有扩展到允许这样做,因此我想知道是否有人知道通过他们的API访问公共Google文档的正确方法?

Google文档列表API(参见ahab的答案)。

您可以简单地检索公共页面,然后解析行的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
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
电子表格网格-列标签:

<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>
请注意id参数。您必须从html源代码中提取它,如下所示:

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

我过去常常捕获包括SSL加密消息在内的通信。

我试图读取一个包含多个选项卡的电子表格,而当我实现这些其他答案(即直接向页面发出web请求)时返回的html和javascript几乎无法读取。在进一步挖掘之后,我发现如果你真的想在不必处理任何OAuth内容的情况下获得一个公共Google电子表格的值,你首先需要转到,创建一个项目,转到仪表板,转到凭据,单击“创建凭据”,并设置一个新的服务帐户密钥(选择json并保存json文件。然后您可以使用它获取任何公共电子表格):

<td class="g-0-GjugEgs2">3600001</td>
<td class="g-0-GjugEgs2">2012</td>
<td class="g-0-GjugEgs2">2013</td>
</tr>
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
 ...
 var mergedConfig = {"formToken":"Kg2uOS1UniIe0yFks5zcDZDsGQ=","formStatus":false,"id":"tA6wDCJME0C-F342ieQghNA.PREF_08255578241116458508.3736592253424693644",
 ...
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();
}