使用谷歌驱动器sdk的例子来阅读电子表格

时间:2015-10-20 08:57:41

标签: c# .net google-sheets google-drive-api

我正在尝试使用google drive sdk exmple来阅读电子表格。

当我打开示例时,我收到此错误:"未处理的删除已发生.........返回意外结果" 404"

我正在做以下事情: 1)在登录部分我正确输入我的用户名和密码(验证它几次是正确的) 2)转到标签:"选择SpreadSheet"。然后出现错误

1 个答案:

答案 0 :(得分:0)

您遇到的问题与此问题类似:Google drive API to C#

您无法再使用旧用户凭据(仅限用户名/密码)登录Google Spreadsheets。您现在需要使用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();
        }
    }
}