使用 C# 访问私人 Google 电子表格

Tom*_*omo 5 .net c# json oauth-2.0 google-spreadsheet-api

我有一个 AngularJS + C#.NET OnePage 应用程序网站。目标是从我使用 C#(而不是使用 AngularJS)拥有的私有 Google 电子表格中检索 JSON。我阅读了各种 Google Sheets 文档和 API、OAuth 2.0 等,并尝试了一些示例,但它们似乎都不适合我。我看到有不同的方法可以从 Google 电子表格访问和检索数据,但它们仍然不适用于我的情况。谁能帮我?谢谢你。

编辑:我设法通过在 Google Developers Console=>Credentials=>Create Client ID 下创建其他应用程序类型来获得令牌。这是 C# 控制台应用程序:

using System;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace MySpreadsheetIntegration
{
  class Program
  {
    static void Main(string[] args)
    {
      ////////////////////////////////////////////////////////////////////////////
      // STEP 1: Configure how to perform OAuth 2.0
      ////////////////////////////////////////////////////////////////////////////

      // TODO: Update the following information with that obtained from
      // https://code.google.com/apis/console. After registering
      // your application, these will be provided for you.

      string CLIENT_ID = "12345678.apps.googleusercontent.com";

      // This is the OAuth 2.0 Client Secret retrieved
      // above.  Be sure to store this value securely.  Leaking this
      // value would enable others to act on behalf of your application!
      string CLIENT_SECRET = "Gc0230jdsah01jqpowpgff";

      // Space separated list of scopes for which to request access.
      string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";

      // This is the Redirect URI for installed applications.
      // If you are building a web application, you have to set your
      // Redirect URI at https://code.google.com/apis/console.
      string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

      ////////////////////////////////////////////////////////////////////////////
      // STEP 2: Set up the OAuth 2.0 object
      ////////////////////////////////////////////////////////////////////////////

      // OAuth2Parameters holds all the parameters related to OAuth 2.0.
      OAuth2Parameters parameters = new OAuth2Parameters();

      // Set your OAuth 2.0 Client Id (which you can register at
      // https://code.google.com/apis/console).
      parameters.ClientId = CLIENT_ID;

      // Set your OAuth 2.0 Client Secret, which can be obtained at
      // https://code.google.com/apis/console.
      parameters.ClientSecret = CLIENT_SECRET;

      // Set your Redirect URI, which can be registered at
      // https://code.google.com/apis/console.
      parameters.RedirectUri = REDIRECT_URI;

      ////////////////////////////////////////////////////////////////////////////
      // STEP 3: Get the Authorization URL
      ////////////////////////////////////////////////////////////////////////////

      // Set the scope for this particular service.
      parameters.Scope = SCOPE;

      // Get the authorization url.  The user of your application must visit
      // this url in order to authorize with Google.  If you are building a
      // browser-based application, you can redirect the user to the authorization
      // url.
      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();

      ////////////////////////////////////////////////////////////////////////////
      // STEP 4: Get the Access Token
      ////////////////////////////////////////////////////////////////////////////

      // Once the user authorizes with Google, the request token can be exchanged
      // for a long-lived access token.  If you are building a browser-based
      // application, you should parse the incoming request token from the url and
      // set it in OAuthParameters before calling GetAccessToken().
      OAuthUtil.GetAccessToken(parameters);
      string accessToken = parameters.AccessToken;
      Console.WriteLine("OAuth Access Token: " + accessToken);

      ////////////////////////////////////////////////////////////////////////////
      // STEP 5: Make an OAuth authorized request to Google
      ////////////////////////////////////////////////////////////////////////////

      // Initialize the variables needed to make the request
      GOAuth2RequestFactory requestFactory =
          new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
      SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
      service.RequestFactory = requestFactory;

      Console.ReadLine();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

使用此代码,我必须复制我获得的链接并将其粘贴到浏览器中以获取令牌。有没有办法直接在我的应用程序中获取此令牌而无需手动复制链接?

Mar*_*rov 1

有一种方法可以使用证书进行服务器到服务器的通信,而不是复制/粘贴 Google AccessCode 提供的证书来建立通信。

首先,您需要从Google Console获取证书。如果您尚未创建项目,请创建项目并为其命名。然后:

  1. 转到“凭据”部分(位于左侧菜单中)并
  2. 单击“创建凭据”按钮并选择“服务帐户密钥”
  3. 选择服务帐户为“App Engine 默认服务帐户”并
  4. 选择“密钥类型”为“p.12”,即证书类型。

参考图片:

在此输入图像描述

完成这些步骤后,证书将自动下载。将其上传到项目中的某个文件夹中并按如下所示使用它。

注意:为了简单起见,代码放置在控制器中。

    public async Task<ActionResult> ServerAuth()
    {
        ViewBag.Message = "Server to server authentication";
        List<string> records = new List<string>();

        const string ServiceAccountEmail = "your-account@appspot.gserviceaccount.com";
        string fullKeyPath = HttpContext.Server.MapPath("~/Key/MyProjectKey.p12"); // The certificate generated by Google and uploaded in the project.

        var certificate = new X509Certificate2(fullKeyPath, "notasecret", X509KeyStorageFlags.Exportable); // "notasecret" is the password for the certificate

        var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(ServiceAccountEmail)
        {
            Scopes = new[] { "https://spreadsheets.google.com/feeds", "http://spreadsheets.google.com/feeds/spreadsheets/private/full" }  
        }.FromCertificate(certificate);

        var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);

        if (!await credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None))
        {
            throw new InvalidOperationException("Access token request failed.");
        }

        var requestFactory = new GDataRequestFactory(null);
        requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken);

        var service = new SpreadsheetsService(null) { RequestFactory = requestFactory };

        SpreadsheetQuery query = new SpreadsheetQuery();
        query.Title = "Test Sheet"; // The exact name of the sheet you want to read
        query.Exact = true;

        var feed = service.Query(query);

        foreach (SpreadsheetEntry entry in feed.Entries)
        {
            foreach (WorksheetEntry worksheet in entry.Worksheets.Entries.Cast<WorksheetEntry>())
            {
                CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
                CellFeed cellFeed = service.Query(cellQuery);

                foreach (CellEntry cell in cellFeed.Entries)
                {
                    records.Add(cell.InputValue);
                }
            }
        }

        return View(records);
    }
Run Code Online (Sandbox Code Playgroud)