C# 4.0 '的类型初始值设定项;Google.api.Json.NewtonsoftJsonSerializer';抛出异常
我正在使用Oauth 2访问google分析数据。当我尝试执行以下代码时,会发生此错误:C# 4.0 '的类型初始值设定项;Google.api.Json.NewtonsoftJsonSerializer';抛出异常,c#-4.0,oauth-2.0,C# 4.0,Oauth 2.0,我正在使用Oauth 2访问google分析数据。当我尝试执行以下代码时,会发生此错误: 'Google.api.Json.NewtonsoftJsonSerializer'的类型初始值设定项 抛出了一个异常 这是我的密码: string clientid = "my client id"; string clientsecret = "my client secret"; var client = new WebServerClient(GoogleAuthenticationServer.
'Google.api.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;
}
string clientid=“我的客户id”;
string clientsecret=“我的客户机密”;
var client=新的WebServerClient(GoogleAuthenticationServer.Description,clientid,clientsecret);
var auth=新的OAuth2Authenticator(客户端,身份验证);
var asv=new Google.api.Analytics.v3.AnalyticsService(new BaseClientService.Initializer())
{
Authenticator=auth,
});
var请求=asv.Data.Ga.Get(“Ga:”+“我的档案ID”,“2012-01-01”,“2012-02-20”,“Ga:访问”);
var report=request.Fetch();
私有IAAuthorizationState身份验证(WebServerClient)
{
IAAuthorizationState=新授权状态(新字符串[]{}{RefreshToken=“我的刷新令牌”};
client.RefreshToken(状态);
返回状态;
}
我使用Newtonsoft.Json导入的名称空间
这是旧版本。然后我导入了新版本的
Newtonsoft.Json
(version=4.0.2.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;
}
static void Main(字符串[]args)
{
尝试
{
//通过提供您的应用程序clientid,client secretid来设置Web服务器客户端,我们在google api(云)控制台中注册了该应用程序。
var client=新的WebServerClient(GoogleAuthenticationServer.Description,“您的客户ID”,“客户机密”);
//通过传递长寿命刷新令牌对Web服务器客户端和GA帐户进行身份验证
var auth=新的OAuth2Authenticator(客户端,身份验证);
//初始化谷歌分析服务。
//在Google analytics服务实例中将auth参数设置为Authenticator
var asv=new Google.api.Analytics.v3.AnalyticsService(new BaseClientService.Initializer())
{
验证器=auth
});
//准备Google api的查询请求
字符串queryDate=“”;
queryDate=DateTime.Today.AddDays(-1).ToString(“yyyy-MM-dd”);
var request=asv.Data.Ga.Get(“Ga:“+”您的profileid”,queryDate,queryDate,“Ga:visions,Ga:newvisions,Ga:bounces,Ga:pageviews,Ga:timesite,Ga:transactionpervisit”);
//添加维度
request.Dimensions=“ga:日期”;
//粪便数据
var data=request.Fetch();
}
捕获(例外情况除外)
{
}
}
私有静态IAuthorizationState身份验证(WebServer客户端)
{
//应用程序捕获的刷新令牌称为长寿命刷新令牌,将来将用于gat新访问令牌。
//当用户允许我们的应用程序访问他的分析数据时,Json格式的访问令牌和刷新令牌将被捕获并保存在数据库中。
//下次我们要访问该用户的google analytics数据时,请加载刷新令牌
IAAuthorizationState=新授权状态(新字符串[]{}{RefreshToken=“您的刷新令牌”};
//通过传递长寿命刷新令牌刷新访问令牌
client.RefreshToken(状态);
//返回IAutherization结果
返回状态;
}
注意:在使用这些方法之前,您必须先获取刷新令牌我已经记录了我在
YouTube Data API v3
中解决此问题的经验,请参阅下面的完整详细信息:
仅供参考,我正在使用Visual Studio 2012,.Net解决方案由Asp.Net
Web应用程序和类库项目组成。我在类库项目中引用了googledataapi
v3
- 确保您已从Nuget manager安装了最新的
客户端库Google.api.YouTube.v3
- 从
管理器更新所有挂起的包Nuget
- 从bin文件夹(两个项目)中删除所有文件
- 从web应用程序项目中删除
引用(如果存在)Newtonsoft.Json
- 从web应用程序项目的
中删除以下行(如果存在):packages.config
<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>
- 我有这个错误,但我的解决方案有点不同。我使用的是VisualStudio2017,我安装了Newtonsoft.Json的当前版本(当时为11.0.2)。当我安装当前版本的Google.api(1.35.1)时,我收到了错误“Google.api.Json.newtonsoftjsonalizer的类型初始值设定项引发了异常”。当查看内部异常信息时,我发现Google.api预期的是Newtonsoft.Json版本10。我删除了Newtonsoft和Google.api,然后通过nuget重新安装了Google.api。还安装了Newtonsoft.Json的版本10,错误消失了
如果任何人也有此错误,请确保实际安装了google api预期的Newtonsoft版本。堆栈跟踪应显示一个内部异常,解释在类型初始值设定项期间引发的异常。将其编辑到您的问题中(同时整理代码格式)。InnerException={“无法加载文件或程序集'Newtonsoft.Json.Net35,Version=4.0.2.0,Culture=neutral,PublicKeyToken=null'或其依赖项之一。系统找不到指定的文件。”:“Newtonsoft.Json.Net35,版本=4.0.2.0,区域性=中性,PublicKeyToken。。。