使用c#更新一个Google电子表格

本文关键字:一个 Google 电子表格 更新 使用 | 更新日期: 2023-09-27 18:19:16

这是我第一次在这里发帖,我刚开始使用Google API。我遇到的问题是我试图登录到我的谷歌驱动器帐户和更新电子表格中的特定单元格。我能够登录到我的谷歌驱动器,但当我试图访问谷歌电子表格,我得到一个"解析失败"的错误。

using System;
using Google;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
            {ClientId = "XXXXX.apps.googleusercontent.com",ClientSecret = "XXXXX", }, new[] { DriveService.Scope.Drive }, "user", CancellationToken.None).Result;

        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });
SpreadsheetsService services = new SpreadsheetsService("site");
SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = services.Query(query);`

当我到达services.Query(query)时,我得到了解析失败的错误。如有任何帮助,不胜感激。

这里是完整的错误

Google.GData.Client.ClientFeedException was unhandled
 HResult=-2146233088
 Message=Parsing failed
 Source=Google.GData.Client
 StackTrace:
   at Google.GData.Client.AtomFeedParser.Parse(Stream streamInput, AtomFeed feed)
   at Google.GData.Client.AtomFeed.Parse(Stream stream, AlternativeFormat format)
   at Google.GData.Client.Service.CreateAndParseFeed(Stream inputStream, Uri uriToUse)
   at Google.GData.Client.Service.Query(FeedQuery feedQuery)
   at Google.GData.Spreadsheets.SpreadsheetsService.Query(SpreadsheetQuery feedQuery)
   at webPull.Program.Main(String[] args) in C:'Users'shartnett'Documents'Visual Studio 2010'Projects'webPull'webPull'Program.cs:line 80
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
 InnerException: System.Xml.XmlException
   HResult=-2146232000
   Message=Root element is missing.
   Source=System.Xml
   LineNumber=0
   LinePosition=0
   SourceUri=""
   StackTrace:
        at System.Xml.XmlTextReaderImpl.Throw(Exception e)
        at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
        at System.Xml.XmlTextReaderImpl.Read()
        at System.Xml.XmlTextReader.Read()
        at Google.GData.Client.BaseFeedParser.MoveToStartElement(XmlReader reader)
        at Google.GData.Client.AtomFeedParser.Parse(Stream streamInput, AtomFeed feed)
   InnerException: 

使用c#更新一个Google电子表格

这里有一篇文章讨论了"解析失败"的问题。

它提到在Fiddler中看到对http://www.w3.org/TR/html4/strict.dtd的调用失败。

尝试使用Fiddler,看看哪些调用可能失败