googledriveapi到C#
您好,我正试图阅读一个电子表格使用谷歌驱动API使用谷歌API的例子 然而,我得到了同样的失败:“Google.GData.Client.GDataRequestException类型的未处理异常在Google.GData.Client.dll中发生 其他信息:执行身份验证请求返回意外结果:404“ 我确信我输入了正确的登录名和密码 代码如下:googledriveapi到C#,c#,google-sheets,google-drive-api,C#,Google Sheets,Google Drive Api,您好,我正试图阅读一个电子表格使用谷歌驱动API使用谷歌API的例子 然而,我得到了同样的失败:“Google.GData.Client.GDataRequestException类型的未处理异常在Google.GData.Client.dll中发生 其他信息:执行身份验证请求返回意外结果:404“ 我确信我输入了正确的登录名和密码 代码如下: /// <summary> /// Interaction logic for MainWindow.xaml /// </summa
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SpreadsheetsService myService = new SpreadsheetsService("exampleCo-exampleApp-1");
myService.setUserCredentials(myusername, password);
SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = myService.Query(query);
Console.WriteLine("Your spreadsheets:");
foreach (SpreadsheetEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
}
}
}
//
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
电子表格服务myService=新的电子表格服务(“exampleCo-exampleApp-1”);
setUserCredentials(我的用户名、密码);
电子表格查询=新建电子表格查询();
SpreadsheetFeed=myService.Query(查询);
Console.WriteLine(“您的电子表格:”);
foreach(提要中的电子表格条目。条目)
{
Console.WriteLine(entry.Title.Text);
}
}
}
GDataAPI中的旧身份验证方法已被弃用。您需要使用OAuth2
此示例要求您使用以下nuget包及其依赖项:
- Google.GData.Spreadsheets
你能分享你用来认证自己的代码吗?如果您遵循本教程,那么应该在client_secret.json文件中注意您的身份验证。驱动器中的错误404通常表示找不到文件,但错误消息似乎与身份验证有关。我更新了代码。仍然是相同的错误。仍然存在相同错误的问题。
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();
}
}
}