我已經按照文檔列表(用於創建spreadhseet)和電子表格(用於創建工作表和添加行)的整個API。 但是,我能夠在創建後到工作表添加到等試算表,但是當我嘗試添加該行,我得到的錯誤錯誤時拋出:執行的請求失敗:https://spreadsheets.google.com/feeds/list/tj0pIc6qpEB2LtZY9mwfT-A/od6/private/full在向Google電子表格添加行時出現異常
我所提到的所有的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流量並檢查響應的詳細信息 –
在內部異常下拉菜單中,它表示{「遠程服務器返回了一個錯誤:(400)錯誤請求。」}作爲響應,StatusCode作爲System .Net.HttpStatusCode.BadRequest。這是谷歌服務器錯誤,或者我錯過了什麼? – Vacca
我甚至嘗試過在電子表格上手動編寫行後刪除和行更新,但只在電子表格API上添加行功能不工作,我不認爲他們似乎是我的代碼中的任何錯誤,因爲行更新,刪除工作正常。迴應字符串是:我們'對不起,發生服務器錯誤。請稍等,然後嘗試重新加載電子表格。 - – Vacca