使用ServiceAccount实施的C#Google电子表格服务

时间:2015-06-29 13:01:55

标签: c# google-api google-spreadsheet-api service-accounts

什么是"我的应用用户代理"在GDataRequestFactory中。我已经完成了上面提到的代码,但仍然没有获得电子表格。我无法访问Google文档中的电子表格数据。

if (!File.Exists(keyFilePath))
    {
        Console.WriteLine("An Error occurred - Key file does not exist");
        return null;
    }

    //Google Drive scopes Documentation:   https://developers.google.com/drive/web/scopes
    string[] scopes = new string[] { "https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds" };  // modify your app scripts     

    var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
    try
    {
        var serviceAccountCredentialInitializer =
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = new[] { "https://spreadsheets.google.com/feeds" }
            }.FromCertificate(certificate);

        var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);

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

        credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait();

        var requestFactory = new GDataRequestFactory("My App User Agent");
        requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken);

        SpreadsheetsService service = new SpreadsheetsService("CallLog");

        service.RequestFactory = requestFactory;

        SpreadsheetQuery query = new SpreadsheetQue`enter code here`ry(); //do the job as you done it before
        SpreadsheetFeed feed = service.Query(query);

0 个答案:

没有答案