【发布时间】:2012-10-22 09:21:15
【问题描述】:
我已遵循文档列表(用于创建电子表格)和电子表格(用于创建工作表和添加行)的整个 API。 但是,我可以在创建后将工作表添加到电子表格中,但是当我尝试添加行时,我收到错误异常:执行请求失败:https://spreadsheets.google.com/feeds/list/tj0pIc6qpEB2LtZY9mwfT-A/od6/private/full
我已经提到了所有 OAuth 范围和所需的凭据,但无法解决此异常。 其余的工作都很好,比如创建谷歌电子表格和添加工作表。 我只是复制粘贴谷歌代码。
// setUp the confirguration for OAuth 2.0
string clientID = "********.apps.googleusercontent.com";
string clientSecret = "*******************";
string scope = "https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/";
string redirectURI = "urn:***:wg:oauth:2.0:oob";
// setup the OAuth 2.0 object
OAuth2Parameters parameters = new OAuth2Parameters(); // to hold all the parameters
parameters.ClientId = clientID; // setup the clientID
parameters.ClientSecret = clientSecret; // setup the clientSecret
parameters.RedirectUri=redirectURI; // setup the redirectURI
//setup the authurization URL
parameters.Scope = scope; // set the scope
string authorizationURL = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
Console.WriteLine(authorizationURL);
Console.WriteLine("Please visit the URL above to authorize your OAuth " + "request token. Once that is complete, type in your access code to "
+ "continue...");
parameters.AccessCode = Console.ReadLine();
// get the access token
OAuthUtil.GetAccessToken(parameters);
string accessToken = parameters.AccessToken;
Console.WriteLine("Cosole Access token " + accessToken);
//make Auth Request to Google
GOAuth2RequestFactory factory = new GOAuth2RequestFactory(null, "SampleSpreadSheetApp-V1", parameters);
// DocumentsService service = new DocumentsService("SampleSpreadSheetApp-V1");
service.RequestFactory = factory;
//---------------------------------------------------------------------
GOAuth2RequestFactory requestFactory =
new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
service.RequestFactory = requestFactory;
SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = service.Query(query);
if (feed.Entries.Count == 0)
{
Console.WriteLine("no spreadsheets present here");
}
// TODO: Choose a spreadsheet more intelligently based on your
// app's needs.
SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
Console.WriteLine(spreadsheet.Title.Text);
// Get the first worksheet of the first spreadsheet.
// TODO: Choose a worksheet more intelligently based on your
// app's needs.
WorksheetFeed wsFeed = spreadsheet.Worksheets;
WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];
if (wsFeed.Entries.Count == 0)
{
Console.WriteLine("no worksheets present here");
}
// Define the URL to request the list feed of the worksheet.
AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);
// Fetch the list feed of the worksheet.
ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
ListFeed listFeed = service.Query(listQuery);
// Create a local representation of the new row.
ListEntry row = new ListEntry();
row.Elements.Add(new ListEntry.Custom() { LocalName = "firstname", Value = "Joe" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "lastname", Value = "Smith" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "age", Value = "26" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "height", Value = "176" });
// Send the new row to the API for insertion.
service.Insert(listFeed, row);
【问题讨论】:
-
如果您深入了解异常详细信息,您会发现完整的错误消息,告诉您请求失败的原因。作为替代方案,尝试使用 Fiddler 捕获 HTTP 流量并检查响应的详细信息
-
在内部异常下拉列表中,它显示 {"The remote server returned an error: (400) Bad Request."} 作为响应,StatusCode 作为 System.Net.HttpStatusCode.BadRequest。这是谷歌服务器错误还是我遗漏了什么?
-
我什至在手动写入电子表格后尝试了行删除和行更新,但只有在电子表格 API 上添加行功能不起作用,我认为它们在我的代码中似乎没有任何问题,因为行更新,删除工作正常。响应字符串是:我们很抱歉,发生服务器错误。请稍等,然后尝试重新加载您的电子表格。 ——
-
我可以从这个 API 的一部分的谷歌管理员那里得到任何支持,这个 API 是商业/测试版还是处于 alpha 状态?即使在 google 电子表格 api 论坛 link 这个问题是由其他用户发布的,但我们不支持这个 api 的问题。
-
你有什么解决办法吗?
标签: c# google-drive-api google-docs-api google-spreadsheet-api google-cloud-storage