Vac*_*cca 4 c# google-docs-api google-spreadsheet-api google-cloud-storage google-drive-api
我已经跟踪了整个API的文档列表(用于创建spreadhseet)和电子表格(用于创建工作表和添加行).但是,我可以在创建后向spreadhseet添加工作表,但是当我尝试添加行时,我会收到错误消息:请求执行失败:https://spreadsheets.google.com/feeds/list/ tj0pIc6qpEB2LtZY9mwfT-A/OD6 /私营/全
我已经提到了所有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);
Run Code Online (Sandbox Code Playgroud)
我也在努力解决这个问题,我发现了以下内容:
每列都有一个在第一行中指定的名称.在Google API示例中,第一列是"firstname",它在现有工作表的单元格A1中指示.
当您添加行时(如上面粘贴的示例代码中),"LocalName"属性必须完全匹配.还有API小写并从列名中删除空格(天知道为什么??),所以如果你将列名定义为'Name',API会将其小写为'name'并尝试匹配它.因此,当您添加一行时,您需要在代码中定义小写的列名称,无空格.
为了安全起见,创建一个包含一列的工作表,并将第一行单元格设置为"名称".现在使用您的身份验证运行示例代码,并尝试添加只有一个条目的行,如下所示:
ListEntry row = new ListEntry();
row.Elements.Add(new ListEntry.Custom() { LocalName = "name", Value = "John" });
Run Code Online (Sandbox Code Playgroud)
这不会奏效
row.Elements.Add(new ListEntry.Custom() { LocalName = "Name", Value = "John" });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4186 次 |
| 最近记录: |