Google表格Api v4快速入门教程 - 即使重定向

时间:2018-01-31 06:56:07

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

我正在处理一个项目,该项目要求我从谷歌表中读取和写入数据。因此,我开始使用 Google Sheets Api (v4) Quick start Tutorial进行C#。我使用Visual Studio 2017并拥有所有nuget软件包的最新版本。

我已逐步遵循教程,但我创建了一个Windows窗体应用程序而不是创建控制台应用程序。完全遵循编写的代码和其他步骤。

在IDE中运行时,代码按预期运行。但是,当发布然后在IDE外部运行时,系统找不到System.Net.HTTP。



An unhandled exception of type 'System.IO.FileNotFoundException' occurred in WindowsFormsApp1.exe
Could not load file or assembly 'System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.The system cannot find the file specified.




在研究并尝试在其他类似问题(不起作用)中发布的解决方案之后,我登陆了StackOverflow帖子,建议将旧版本的System.Net.Http重定向到最新版本。但是,这不起作用。

App.config with redirection code (screenshot)

这是我目前的代码:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.IO;
using System.Threading;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
        static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
        static string ApplicationName = "Google Sheets API .NET Quickstart";
        public Form1()
        {
            InitializeComponent();
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Google Sheets API service.
            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // Define request parameters.
            String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
            String range = "Class Data!A2:E";
            SpreadsheetsResource.ValuesResource.GetRequest request =
                    service.Spreadsheets.Values.Get(spreadsheetId, range);

            // Prints the names and majors of students in a sample spreadsheet:
            // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
            ValueRange response = request.Execute();
            IList<IList<Object>> values = response.Values;
            if (values != null && values.Count > 0)
            {
                Console.WriteLine("Name, Major");
                foreach (var row in values)
                {
                    // Print columns A and E, which correspond to indices 0 and 4.
                    Console.WriteLine("{0}, {1}", row[0], row[4]);
                }
            }
            else
            {
                Console.WriteLine("No data found.");
            }
            Console.Read();


        
    }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
&#13;
&#13;
&#13;

任何帮助解决这个问题都将非常感激。谢谢!

0 个答案:

没有答案