在阅读Google电子表格时遇到了例外情况

时间:2017-05-25 06:00:17

标签: c# google-sheets x509certificate x509certificate2

我是初学者在我的项目中使用服务帐户阅读谷歌传播。几天它工作正常,但现在我有一个例外

  

System.Security.Cryptography.CryptographicException:无效的提供程序   指定的类型。

这是堆栈跟踪

at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
at System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()
at Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer.FromCertificate(X509Certificate2 certificate) 

通过查看此堆栈跟踪,我发现它无法读取证书。

这是我的代码:

string applicationName =“QA Automation”;

       var serviceaccountemail = "Myaccount.gserviceaccount.com";

        var certificate = new X509Certificate2(GetCertificateBytes(), "*****", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
          new ServiceAccountCredential.Initializer(serviceaccountemail)
          {
              Scopes = new[] { "https://spreadsheets.google.com/feeds" }
          }.FromCertificate(certificate));

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

        String spreadsheetId = "**************************************";//spreadId

        String range = "Sheet1!A1:C";

        SpreadsheetsResource.ValuesResource.GetRequest request = 
            service.Spreadsheets.Values.Get(spreadsheetId, range);

        ValueRange response = request.Execute();

.FromCeritificate(证书)获得了异常。我已经研究过这个例外“无效的提供者类型”。我得到了结果,因为它是IIS的许可。还有其他解决办法吗?

提前致谢。

0 个答案:

没有答案