ServiceCredential.cs 未在 googleAnalyticApi 中找到

ServiceCredential.cs not found in googleAnalyticApi

我试图从 google 分析 api 检索数据,这是我的 class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.Apis.Analytics.v3;
using Google.Apis.Auth.OAuth2;
using System.Threading;
using Google.Apis.Util.Store;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Services;

namespace zirfoon.model
{
    public class googleanalyze
    {
public static void main(){
    string[] scopes = new string[] { AnalyticsService.Scope.Analytics,AnalyticsService.Scope.AnalyticsManageUsers }; // view and manage your Google Analytics data

    var keyFilePath = HttpContext.Current.Server.MapPath("/loominexMvc-f104810a4caa.p12");    // Downloaded from https://console.developers.google.com
    var serviceAccountEmail = "loominexmvc@appspot.gserviceaccount.com";  // found https://console.developers.google.com

    //loading the Key file
    var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
    var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes =scopes
    }.FromCertificate(certificate));

}
    }
} 

当我到达下面的行时

var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
    Scopes =scopes 
}.FromCertificate(certificate));

visual studio 调试器打开一个 windows 到 select ServiceCredential.cs 但它不存在,我改变了范围但仍然没有... 我已经正确地创建了我的 api 帐户,并且 serviceAccountEmailkeyFilePath 等所有信息都是正确的,但我仍然收到错误

您的代码与我通常使用的略有不同。

身份验证

string[] scopes = new string[] {AnalyticsService.Scope.Analytics}; // view and manage your Google Analytics data

var keyFilePath = @"c:\file.p12" ;    // Downloaded from https://console.developers.google.com
var serviceAccountEmail = "xx@developer.gserviceaccount.com";  // found https://console.developers.google.com

//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail) {
                                                   Scopes = scopes}.FromCertificate(certificate));

创建服务

var service = new AnalyticsService(new BaseClientService.Initializer() { HttpClientInitializer = credential,
                                                                         ApplicationName = "Analytics API Sample",});

我会仔细检查您到 keyFilePath 的映射是否正确。

从我的教程中窃取的代码Google Analytics API authentcation

我发现问题只存在于调试模式 当我 运行 在没有调试器的情况下进行项目时,没有任何问题...