c# - 如何使用 C# 和 Sheet Api 访问和插入数据到 Google Spread Sheet?

标签 c# oauth google-api google-sheets google-sheets-api

有人可以帮助我如何访问 Google Spread Sheet Data 使用那里的Google Sheet API 使用oAuth 2.0 也可以在访问工作表时如何插入/添加在线数据

在 .NET C# 中? 我找到了这个 Link但它使用的是 oauth 1 并且只访问数据 请提供示例或链接

最佳答案

我发现以下代码在列中访问和写入值:

string CLIENT_ID = "***";

        string 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);
        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();

        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);

        //foreach (SpreadsheetEntry entry in feed.Entries)
        //{
        //    Console.WriteLine(entry.Title.Text);
        //}

        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];

        // 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);


        CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
        CellFeed cellFeed = service.Query(cellQuery);

        CellEntry cellEntry = new CellEntry(1, 1, "firstname");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 2, "lastname");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 3, "age");

        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 4, "height");
        cellFeed.Insert(cellEntry);

        // 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);

关于c# - 如何使用 C# 和 Sheet Api 访问和插入数据到 Google Spread Sheet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33651570/

相关文章:

java - 如何使用java客户端访问Google服务?

google-api - 谷歌群组设置 API 的域范围委派身份验证?

c# - 如何取消 CSharpScript.RunAsync

c# - 转换日期时间

c# - 无懈可击的 XMLException

oauth - Linkedin OAuth 2.0 访问 token 响应不完整

php - 升级 laravel 5.1 - 5.4 时如何修复 'You must set the encryption key going forward to improve the security of this library' 错误

java - token 响应异常 : 401 Unauthorized Exception when trying to access Admin SDK Google API.

javascript - 我没有收到 Google Books API 的响应?

c# - 从 JSON 编码的二进制数据恢复图像