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

时间:2016-02-11 14:54:53

标签: c# .net json oauth-2.0 google-spreadsheet-api

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

编辑:我设法通过在Google Developers Console下创建其他应用程序类型来获取令牌=>凭据=>创建客户端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();
    }
  }
}

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

2 个答案:

答案 0 :(得分:3)

我找到了另一种有效的解决方案,您无需打开浏览器窗口,该窗口类似于上面使用p12键的解决方案。

首先,在创建凭证服务帐户密钥下的Google Developer Console中创建。

然后,您必须选择服务帐户类型,您可以选择 App Engine默认服务帐户,然后您可以下载包含私钥的json文件。

使用此文件,您可以以编程方式创建Google凭据(其中" sheets-test.json"是下载的json文件):

var credential = GoogleCredential.FromStream(new FileStream("Sheets-test.json", FileMode.Open)).CreateScoped(Scopes);

授予服务用户(您可以在 client_email 字段的json文件中找到它)访问您的电子表格。

以下是完整代码,可让您将值附加到电子表格中的第一个空闲行,我修改了google .NET quickstart project

using System;
using System.Collections.Generic;
using System.IO;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;

namespace SheetApiTest
{
    public class SheetApiWithGoogleCredentials
    {
        static string[] Scopes = { SheetsService.Scope.Spreadsheets };
        static string ApplicationName = "Google Sheets API .NET Quickstart";

        public void AppendData()
        {
            // the downloaded jsonn file with private key
            var credential = GoogleCredential.FromStream(new FileStream("Sheets-test.json", FileMode.Open)).CreateScoped(Scopes);

            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // spreadsheet id - your own spreadsheet id
            var spreadsheetId = "11AwV7d1pEPq4x-rx9WeZHNwGJa0ehfRhh760";

            var valueRange = new ValueRange { Values = new List<IList<object>> { new List<object>() } };
            valueRange.Values[0].Add(DateTime.Now.ToLongTimeString());

            // insert here the name of your spreadsheet table
            var rangeToWrite = "Tabellenblatt1";

            var appendRequest = service.Spreadsheets.Values.Append(valueRange, spreadsheetId, rangeToWrite);
            appendRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED;
            var appendReponse = appendRequest.Execute();
        }
    }
}

答案 1 :(得分:1)

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

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

  1. 转到“凭据”部分(位于左侧菜单中)和
  2. 点击“创建凭据”按钮,然后选择“服务帐户密钥”
  3. 选择服务帐户为“App Engine默认服务帐户”和
  4. 选择“密钥类型”为“p.12”,即证书类型。
  5. 供参考的图片:

    enter image description here

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

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

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