【问题标题】:using the exmple of google drive sdk for read spreadsheet使用 google drive sdk 示例读取电子表格
【发布时间】:2015-10-20 08:57:41
【问题描述】:
我正在尝试使用 google drive sdk 示例来读取电子表格。
当我打开示例时,我收到此错误:“发生了未处理的异常............返回了意外的结果”404”
我正在做以下事情:
1)在登录部分我正确输入了我的用户名和密码(验证几次它是正确的)
2)转到选项卡:“选定的电子表格”。然后出现错误
【问题讨论】:
标签:
c#
.net
google-sheets
google-drive-api
【解决方案1】:
你遇到的问题和这个问题类似:Google drive API to C#
您无法再使用旧的用户凭据(仅限用户名/密码)登录 Google 电子表格。您现在需要使用 OAuth 2.0(这需要您在 console.developers.google.com 创建应用和凭据)。
您可以将以下示例用于身份验证逻辑,并使用此问题中找到的逻辑中的逻辑来实际操作文件:
Accessing Google Spreadsheets with C# using Google Data API
这是我对链接问题的回答,以防将来被删除:
此示例要求您使用以下 nuget 包及其依赖项:
- Google.GData.Spreadsheets
此外,您必须转到 https://console.developers.google.com 并注册您的应用程序并为其创建凭据,以便您可以输入您的 CLIENT_ID 和 CLIENT_SECRET。
这是我用来整理这个例子的文档:https://developers.google.com/google-apps/spreadsheets/
using System;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Spreadsheets;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string CLIENT_ID = "YOUR_CLIENT_ID";
string CLIENT_SECRET = "YOUR_CLIENT_SECRET";
string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.ClientId = CLIENT_ID;
parameters.ClientSecret = CLIENT_SECRET;
parameters.RedirectUri = REDIRECT_URI;
parameters.Scope = SCOPE;
string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
MessageBox.Show(authorizationUrl);
Console.WriteLine("Please visit the URL in the message box to authorize your OAuth "
+ "request token. Once that is complete, type in your access code to "
+ "continue...");
parameters.AccessCode = Console.ReadLine();
OAuthUtil.GetAccessToken(parameters);
string accessToken = parameters.AccessToken;
Console.WriteLine("OAuth Access Token: " + accessToken);
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);
// Iterate through all of the spreadsheets returned
foreach (SpreadsheetEntry entry in feed.Entries)
{
// Print the title of this spreadsheet to the screen
Console.WriteLine(entry.Title.Text);
}
Console.ReadLine();
}
}
}