Reputation:
I am accessing google analytics data using Oauth 2. When I try to execute the following code, this error occurs:
The type initializer for
'Google.Apis.Json.NewtonsoftJsonSerializer'
threw an exception.
Here is my code:
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;
}
Upvotes: 4
Views: 7675
Reputation: 1428
I had this error, but my solution was a little different. I was using Visual Studio 2017 and I had the current version (at the time) of Newtonsoft.Json installed (11.0.2). When I installed the current version of the Google.Apis (1.35.1), I received the error "The type initializer for 'Google.Apis.Json.NewtonsoftJsonSerializer' threw an exception". When looking at the inner exception information I found that the Google.Apis was expecting Newtonsoft.Json version 10. I removed Newtonsoft and Google.Apis and then reinstalled google.apis via nuget. Version 10 of Newtonsoft.Json was also installed and the error went away.
If anyone is also having this error, make sure the version of Newtonsoft that is expected by the google api is actually installed.
Upvotes: 0
Reputation:
Go through the below code, which may help you, its working fine for me.
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;
}
Note: you have to get the refresh token before using these methods
Upvotes: 0
Reputation: 21
I have documented my experience to solve this problem for YouTube Data API v3
, please see full details below:
FYI I am using Visual Studio 2012, and .Net solution is consists of Asp.NET
Web Application and Class Library projects. I have referenced Google Data API
v3 in my Class Library project.
Make sure you have installed latest Google.Apis.YouTube.v3
Client Library from the Nuget manager.
Update all pending packages from the Nuget
manager.
Delete all files from the bin folders (from both projects).
Delete Newtonsoft.Json
reference from the web application project if exist.
Delete following line from the packages.config
of web application project if exist:
<package id="Newtonsoft.Json" version="4.0.8" targetFramework="net45" />
Add the following into the web.config of web application project:
<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>
Upvotes: 2
Reputation:
The namespace which i was importing using Newtonsoft.Json
was the older version. Then i imported the new version of Newtonsoft.Json
(Version=4.0.2.0)
. Now its working ...........
Upvotes: 2