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

时间:2015-11-11 13:28:28

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

当访问工作表如何插入/添加时,还可以使用oAuth 2.0使用Google Sheet API帮助我如何访问 Google Spread表数据在线数据

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

1 个答案:

答案 0 :(得分:3)

我找到了以下代码,用于访问和写入列中的值:

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