我们不断收到下面每当我们试图访问API来获取当前用户数错误。我已经尝试了一些东西,但不能得到这条底线。任何人都可以阐明什么是错/丢失任何光线?
我要指出的是,这样运行本地PC上完全没有问题,但未能在服务器上。
以下是错误:
ConnectToAnalytics错误:System.Security.Cryptography.CryptographicException:指定无效的提供程序类型。在System.Security.Cryptography.Utils.CreateProvHandle(CspParameters参数,布尔randomKeyContainer)在System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType关键字类型,CspParameters参数,布尔randomKeyContainer,的Int32 dwKeySize,SafeProvHandle&safeProvHandle,SafeKeyHandle&safeKeyHandle)在System.Security .Cryptography.RSACryptoServiceProvider.GetKeyPair()在System.Security.Cryptography.RSACryptoServiceProvider..ctor(的Int32 dwKeySize,CspParameters参数,布尔useDefaultKeySize)在System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()在Google.Apis.Auth。 OAuth2.ServiceAccountCredential.Initializer.FromCertificate(X509Certificate2证书)在C:\用户\ mdril \文件\ GitHub的\ Google处理API-DOTNET客户端\ SRC \ GoogleApis.Auth.DotNet4 \的OAuth2 \ ServiceAccountCredential.cs:线100在核心。 ConnectToAnalytics()
当我运行的代码将引发此错误:
Public Shared Function GetRealtimeUsers() As String
Try
'realtime on site
Dim gsService As AnalyticsService = Core.ConnectToAnalytics
Dim RequestRealtime As DataResource.RealtimeResource.GetRequest = gsService.Data.Realtime.[Get]([String].Format("ga:{0}", "xxxxx"), "rt:activeUsers")
Dim feed As RealtimeData = RequestRealtime.Execute()
Return Int(feed.Rows(0)(0)).ToString()
Catch ex As Exception
Return "QUOTA USED"
End Try
在这里错误的:RequestRealtime.Execute()
仅供参考 - 这里是连接脚本:
Public Shared Function ConnectToAnalytics() As AnalyticsService
Try
Dim scopes As String() = New String() {AnalyticsService.Scope.Analytics, AnalyticsService.Scope.AnalyticsEdit, AnalyticsService.Scope.AnalyticsManageUsers, AnalyticsService.Scope.AnalyticsReadonly}
Dim keyFilePath = HttpContext.Current.Server.MapPath("\xxx.p12")
Dim serviceAccountEmail = "xxx"
Dim certificate = New X509Certificate2(keyFilePath, "xxxx", X509KeyStorageFlags.Exportable)
Dim credential = New ServiceAccountCredential(New ServiceAccountCredential.Initializer(serviceAccountEmail) With {.Scopes = scopes}.FromCertificate(certificate))
Return New AnalyticsService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = "Client for xxx"})
Catch ex As Exception
Core.SendAdminStatusReport("ConnectToAnalytics error: " & ex.ToString)
Throw ex
End Try
End Function
我发现在这个岗位此修补程序:
https://www.daimto.com/azure-with-service-accounts-in-c/
更改此行
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
至
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
修正了我的问题
它很难知道你的问题是什么,没有看到你的代码,但这里是我的服务帐户authncation码
/// <summary>
/// Authenticating to Google using a Service account
/// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount
/// </summary>
/// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param>
/// <param name="serviceAccountCredentialFilePath">Location of the .p12 or Json Service account key file downloaded from Google Developer console https://console.developers.google.com</param>
/// <returns>AnalyticsService used to make requests against the Analytics API</returns>
public static AnalyticsreportingService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath, string[] scopes)
{
try
{
if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
throw new Exception("Path to the service account credentials file is required.");
if (!File.Exists(serviceAccountCredentialFilePath))
throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
if (string.IsNullOrEmpty(serviceAccountEmail))
throw new Exception("ServiceAccountEmail is required.");
// For Json file
if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
{
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the Analytics service.
return new AnalyticsreportingService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analyticsreporting Service account Authentication Sample",
});
}
else if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".p12")
{ // If its a P12 file
var certificate = new X509Certificate2(serviceAccountCredentialFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
// Create the Analyticsreporting service.
return new AnalyticsreportingService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analyticsreporting Authentication Sample",
});
}
else
{
throw new Exception("Unsupported Service accounts credentials.");
}
}
catch (Exception ex)
{
throw new Exception("CreateServiceAccountAnalyticsreportingFailed", ex);
}
}
}
我设法解决这个问题 - 我是在正确的线路与根证书,原来我缺少一个。这些都可以在这里找到:
我不记得在那里我发现了这一点,但添加下面的代码到webconfig帮我找出我错过了哪个证书:
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.Net">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.HttpListener">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Cache">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add
name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="System.Net.trace.log"
traceOutputOptions = "ProcessId, DateTime"
/>
</sharedListeners>
<switches>
<add name="System.Net" value="Verbose" />
<add name="System.Net.Sockets" value="Verbose" />
<add name="System.Net.Cache" value="Verbose" />
<add name="System.Net.HttpListener" value="Verbose" />
</switches>