4

我正在使用 Oauth 2 访问谷歌分析数据。当我尝试执行以下代码时,会发生此错误:

'Google.Apis.Json.NewtonsoftJsonSerializer' 引发异常的类型初始化程序。

这是我的代码:

string clientid = "my client id";
string clientsecret = "my client secret";

var client = new WebServerClient(GoogleAuthenticationServer.Description, clientid, clientsecret);

var auth = new OAuth2Authenticator<WebServerClient>(client, Authenticate);

var asv = new Google.Apis.Analytics.v3.AnalyticsService(new BaseClientService.Initializer()
{
    Authenticator = auth,
});

var request = asv.Data.Ga.Get("ga:" + "my ProfileID", "2012-01-01", "2012-02-20", "ga:visits");
var report = request.Fetch();

private IAuthorizationState Authenticate(WebServerClient client)
{
    IAuthorizationState state = new AuthorizationState(new string[] {}) { RefreshToken = "my refresh token" };
    client.RefreshToken(state);
    return state;
}
4

4 回答 4

2

我正在导入的命名空间using Newtonsoft.Json 是旧版本。然后我导入了新版本的Newtonsoft.Json (Version=4.0.2.0). 现在它的工作......

于 2013-11-11T09:45:30.133 回答
2

我已经记录了我解决此问题的经验YouTube Data API v3,请参阅下面的完整详细信息:

仅供参考,我使用的是 Visual Studio 2012,.Net 解决方案由Asp.NETWeb 应用程序和类库项目组成。我Google Data API在我的类库项目中引用了 v3。

  • 确保您Google.Apis.YouTube.v3已从 Nuget 管理器安装了最新的客户端库。

  • 从管理器更新所有待处理的包Nuget

  • 从 bin 文件夹中删除所有文件(从两个项目中)。

  • Newtonsoft.Json如果存在,请从 Web 应用程序项目中删除引用。

  • packages.config如果存在,请从 Web 应用程序项目中删除以下行:

    <package id="Newtonsoft.Json" version="4.0.8" targetFramework="net45" />
    
  • 将以下内容添加到 Web 应用程序项目的 web.config 中:

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-4.2.20.0" newVersion="4.2.20.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-1.2.13.0" newVersion="1.2.13.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-1.0.168.0" newVersion="1.0.168.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
    
于 2014-04-30T16:32:20.667 回答
0

浏览下面的代码,这可能会对你有所帮助,它对我来说工作正常。

    static void Main(string[] args)
    {

        try
        {
            // Setting up webserver client by providing your application clientid,client secretid which are we registered in google api (cloud) console.
            var client = new WebServerClient(GoogleAuthenticationServer.Description, "Your ClientID", "Client Secret");

            // Authenticating the Web server client and GA account by passing the long lived refresh token 
            var auth = new OAuth2Authenticator<WebServerClient>(client, Authenticate);

            // Initialize the Google analytics service.
            // Set the auth parameter to Authenticator in Google analytics service instance
            var asv = new Google.Apis.Analytics.v3.AnalyticsService(new BaseClientService.Initializer()
            {
                Authenticator = auth
            });

            // Preparing the query request  for Google api
            string queryDate = "";
            queryDate = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
            var request = asv.Data.Ga.Get("ga:" + "Your profileid", queryDate, queryDate, "ga:visits,ga:newVisits,ga:bounces,ga:pageviews,ga:timeOnSite,ga:transactionsPerVisit");
            // Adding dimensions
            request.Dimensions = "ga:date";
            // Fecthing data
            var data = request.Fetch();



        }

        catch (Exception ex)
        {

        }
    }




    private static IAuthorizationState Authenticate(WebServerClient client)
    {
        // The refresh token which application captures called as long lived refresh token, and will be used for gat new access token in future.
        // In Json format access token and refresh token will be captured and saved in database when user allowd our application to access his analytics data.
        // Next time when we are going to access the google analytics data for that user load the refresh token
        IAuthorizationState state = new AuthorizationState(new string[] { }) { RefreshToken = "your refresh token" };

        // Refresh the access token by passing the long lived refreshed token
        client.RefreshToken(state);

        // return the IAutherizationSate result
        return state;
    }

注意:您必须在使用这些方法之前获取刷新令牌

于 2014-02-13T13:38:24.120 回答
0

我有这个错误,但我的解决方案有点不同。我使用的是 Visual Studio 2017,并且安装了 Newtonsoft.Json 的当前版本(当时)(11.0.2)。当我安装当前版本的 Google.Apis (1.35.1) 时,我收到错误“'Google.Apis.Json.NewtonsoftJsonSerializer' 的类型初始化程序引发异常”。查看内部异常信息时,我发现 Google.Apis 期待 Newtonsoft.Json 版本 10。我删除了 Newtonsoft 和 Google.Apis,然后通过 nuget 重新安装了 google.apis。还安装了 Newtonsoft.Json 的第 10 版并且错误消失了。

如果有人也遇到此错误,请确保实际安装了 google api 预期的 Newtonsoft 版本。

于 2018-08-14T13:54:19.447 回答