GoogleAPI-Godaddy上的服务账户C#
我陷入了一个大问题 我在ASP.NET中构建了一个MVC WebAPI,它使用google api(服务帐户)写入google电子表格。我可以很好地运行这个应用程序,而且我还能够写入电子表格。但问题是,当我把它放在godaddy的共享windows服务器上时,它会给我一个错误GoogleAPI-Godaddy上的服务账户C#,c#,google-api,x509certificate2,service-accounts,C#,Google Api,X509certificate2,Service Accounts,我陷入了一个大问题 我在ASP.NET中构建了一个MVC WebAPI,它使用google api(服务帐户)写入google电子表格。我可以很好地运行这个应用程序,而且我还能够写入电子表格。但问题是,当我把它放在godaddy的共享windows服务器上时,它会给我一个错误 [加密例外: 发生内部错误 ] 系统。安全。加密。加密异常。ThrowCryptographicException(Int32 hr)+33 System.Security.Cryptography.X509Certif
[加密例外: 发生内部错误 ]
系统。安全。加密。加密异常。ThrowCryptographicException(Int32 hr)+33 System.Security.Cryptography.X509Certificates.X509Utils _LoadCertFromFile(字符串文件名、IntPtr密码、UInt32 dwFlags、, 布尔值persistKeySet、SafeCertContextHandle和pCertCtx) +0
System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(字符串 文件名、对象密码、X509keystrageFlags keystrageFlags)+218
System.Security.Cryptography.X509Certificates.X509Certificate.Import(字符串 文件名、字符串密码、X509keystrageFlags keystrageFlags)+33
System.Security.Cryptography.X509Certificates.X509Certificate2.Import(字符串 文件名、字符串密码、X509keystrageFlags keystrageFlags)+33
GAExampleMVC.GoogleAnalytics.GoogleAnalyticsService.GetStats()中的 d:\Aakash\Thermax\webapi\GAExampleMVC\GAExampleMVC\GoogleAnalytics\GoogleAnalytics.cs:50 GAExampleMVC.Controllers.HomeController.Index()位于 d:\Aakash\Thermax\webapi\GAExampleMVC\GAExampleMVC\Controllers\HomeController.cs:16 lambda_方法(闭包、控制器库、对象[])+62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,对象[]参数)+14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary
2参数)+211
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext,ActionDescriptor ActionDescriptor,IDictionary
2
参数)+27System.Web.Mvc.c__显示类15.b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext预文本,Func
1 continuation)+253
System.Web.Mvc.c__显示类17.b__14()
+21 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext,IList
1过滤器,ActionDescriptor ActionDescriptor,
IDictionary`2参数)+189System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,字符串actionName)+324
System.Web.Mvc.Controller.ExecuteCore()+105
我只是不明白为什么它不能在Godaddy上运行,而同样的代码在localhost上运行良好 我猜这与我的代码中无法访问的某个证书存储有关
有谁能帮我知道确切的问题以及如何解决它。我知道这篇文章比较老,但我想我会根据我最近的经验提供一个答案,以防其他人遇到它。据我所知,实际问题是GoDaddy不允许在共享主机上更改应用程序池标识。要从根本上解决问题,必须为应用程序池标识设置“加载用户配置文件”设置。这允许加载p12。然而,正如我所说的,这不是共享托管平台的选项。所以,解决办法 我对这个解决方案的灵感来自。我没有Bouncy Castle的经验,也没有时间学习,所以我想到了这个 首先…获取Google使用开发者控制台提供的p12文件,并将其转换为pk8私钥。我用OpenSSL来做这件事
openssl pkcs12 -in file.p12 -nocerts -out key.pem
openssl pkcs8 -in key.pem -topk8 -out p8key.pem
其次,将生成的pk8密钥保存在应用程序App_数据文件夹中
第三,使用StreamReader从pk8文件中读取密钥,并使用.FromPrivateKey而不是.FromCertificate初始化ServiceAccountCredential
Dim stReader As New System.IO.StreamReader _
(HostingEnvironment.ApplicationPhysicalPath & "[PathToPK8File].pk8")
Dim aLine As String
Dim keyfile As String
While True
aLine = stReader.ReadLine()
If aLine Is Nothing Then
keyfile = keyfile & vbCrLf
Exit While
Else
keyfile = keyfile & aLine & " "
End If
End While
Dim scopes As IList(Of String) = New List(Of String)()
scopes.Add(CalendarService.Scope.CalendarReadonly)
Dim credential As ServiceAccountCredential = New ServiceAccountCredential(
New ServiceAccountCredential.Initializer(serviceEmail) With {
.Scopes = scopes
}.FromPrivateKey(keyfile)) // Use FromPrivateKey instead of FromCertificate
在生产服务器上正确加载所有内容。谢谢,这解决了我在GoDaddy basic hosting上的问题。此外,将.pem文件转换为PKCS8时,可以使用-nocrypt标志删除加密。