Google api 我如何防止这种Google Analytics C#DotNetOpenAuth协议例外?
我在用电话。我遵循示例项目,但我一直从DotNetOpenAuth获得协议异常 以下是我现在拥有的:Google api 我如何防止这种Google Analytics C#DotNetOpenAuth协议例外?,google-api,oauth-2.0,dotnetopenauth,google-api-client,google-api-dotnet-client,Google Api,Oauth 2.0,Dotnetopenauth,Google Api Client,Google Api Dotnet Client,我在用电话。我遵循示例项目,但我一直从DotNetOpenAuth获得协议异常 以下是我现在拥有的: public static void Main( string[] args ) { // Register the authenticator. NativeApplicationClient provider = new NativeApplicationClient( GoogleAuthenticationServer.Description, gaAppId,
public static void Main( string[] args )
{
// Register the authenticator.
NativeApplicationClient provider = new NativeApplicationClient(
GoogleAuthenticationServer.Description, gaAppId, gaSecret );
OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(
provider, GetAuthorization );
AnalyticsService analyticsService =
new AnalyticsService( new BaseClientService.Initializer {
Authenticator = auth,
ApplicationName = @"Test Application",
} );
DataResource.GaResource.GetRequest request = analyticsService.Data.Ga.Get(
gaId, @"2013-09-04", @"2013-09-18", "ga:totalEvents" );
GaData data = request.Execute();
Console.ReadKey();
}
private static IAuthorizationState GetAuthorization( NativeApplicationClient arg )
{
// Get the auth URL:
IAuthorizationState state =
new AuthorizationState( new[] {AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue()} );
state.Callback = new Uri( NativeApplicationClient.OutOfBandCallbackUrl );
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization( authCode, state );
}
这就是谷歌的回报:
HTTP/1.1 400 Bad Request
IRRELEVANT_HEADERS
{
"error" : "invalid_request"
}
我在这里有点不知所措,不知道为什么会发生这样的事情,也不知道如何解决。我找不到其他有效的C#示例。这似乎是一种不同于过去的错误。你们中有谁知道我如何解决这个问题吗?我进一步研究了peleyal提供的示例,并设法找到了问题所在。问题是我实际上并没有存储刷新令牌,而是存储了一个身份验证令牌。我应该使用身份验证令牌来生成刷新令牌 以下是简化的完整解决方案,供将来参考。差异可以在GetAuthorization函数中找到,该函数现在可以正确保存刷新令牌,并在刷新令牌不可用时打开浏览器窗口请求授权
private static refreshToken = null;
public static void Main( string[] args )
{
// Register the authenticator.
NativeApplicationClient provider = new NativeApplicationClient(
GoogleAuthenticationServer.Description, gaAppId, gaSecret );
OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(
provider, GetAuthorization );
AnalyticsService analyticsService = new AnalyticsService( new BaseClientService.Initializer {
Authenticator = auth,
ApplicationName = @"Test Application",
} );
DataResource.GaResource.GetRequest request = analyticsService.Data.Ga.Get( gaId, @"2013-09-04",
@"2013-09-18", @"ga:totalEvents" );
GaData data = request.Execute();
Console.ReadKey();
}
private static IAuthorizationState GetAuthorization( NativeApplicationClient arg )
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(
new[] {AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue()} );
state.Callback = new Uri( NativeApplicationClient.OutOfBandCallbackUrl );
if( !string.IsNullOrEmpty( refreshToken ) ) {
try {
state.RefreshToken = refreshToken;
arg.RefreshToken( state );
} catch {
refreshToken = null;
}
}
// If the refresh token is empty, request a new one by opening
// a browser window. Allows the user to paste its authorization token
// and saves the refresh token.
if( string.IsNullOrEmpty( refreshToken ) ) {
Uri authUri = arg.RequestUserAuthorization( state );
// Request authorization from the user (by opening a browser window):
Process.Start( authUri.ToString() );
Console.Write( " Authorization Code: " );
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
state = arg.ProcessUserAuthorization( authCode, state );
refreshToken = state.RefreshToken;
}
return state;
}
private static refreshttoken=null;
公共静态void Main(字符串[]args)
{
//注册验证器。
NativeApplicationClient提供程序=新的NativeApplicationClient(
GoogleAuthenticationServer.Description,gaAppId,gaSecret);
OAuth2Authenticator auth=新的OAuth2Authenticator(
提供商,获取授权);
AnalyticsService AnalyticsService=新的AnalyticsService(新的BaseClientService.Initializer{
Authenticator=auth,
ApplicationName=@“测试应用程序”,
} );
DataResource.GaResource.GetRequest请求=analyticsService.Data.Ga.Get(gaId,@“2013-09-04”,
@“2013-09-18”,简称“ga:totalEvents”);
GaData data=request.Execute();
Console.ReadKey();
}
私有静态IAAuthorizationState GetAuthorization(NativeApplicationClient参数)
{
//获取身份验证URL:
IAAuthorizationState=新授权状态(
新[]{AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue()});
state.Callback=新Uri(NativeApplicationClient.OutOfBandCallbackUrl);
如果(!string.IsNullOrEmpty(refreshToken)){
试一试{
state.RefreshToken=RefreshToken;
参数刷新令牌(状态);
}抓住{
refreshToken=null;
}
}
//如果刷新令牌为空,请通过打开
//浏览器窗口。允许用户粘贴其授权令牌
//并保存刷新令牌。
if(string.IsNullOrEmpty(refreshToken)){
Uri authUri=arg.RequestUserAuthorization(状态);
//请求用户授权(通过打开浏览器窗口):
Process.Start(authUri.ToString());
控制台。写入(“授权代码:”);
字符串authCode=Console.ReadLine();
Console.WriteLine();
//使用授权代码检索访问令牌:
state=arg.ProcessUserAuthorization(authCode,state);
refreshToken=state.refreshToken;
}
返回状态;
}
您是否尝试在我们的样本存储库中运行我们的样本,比方说(任务样本-)。它应该适合你,按照自述说明,让我知道。工作完成后,检查工作示例和代码之间的差异。祝你好运@peleyal我运行了存储库中的样本,它们似乎工作正常。我的代码中一定有个bug。。。我注意到,该示例没有使用脱机刷新令牌,而使用刷新令牌(通过向授权请求url添加access\u type=offline&approval\u prompt=force)不起作用。我将仔细查看这些示例,看看是否可以找到差异,或者是否可以找到使用刷新令牌的示例。
private static refreshToken = null;
public static void Main( string[] args )
{
// Register the authenticator.
NativeApplicationClient provider = new NativeApplicationClient(
GoogleAuthenticationServer.Description, gaAppId, gaSecret );
OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(
provider, GetAuthorization );
AnalyticsService analyticsService = new AnalyticsService( new BaseClientService.Initializer {
Authenticator = auth,
ApplicationName = @"Test Application",
} );
DataResource.GaResource.GetRequest request = analyticsService.Data.Ga.Get( gaId, @"2013-09-04",
@"2013-09-18", @"ga:totalEvents" );
GaData data = request.Execute();
Console.ReadKey();
}
private static IAuthorizationState GetAuthorization( NativeApplicationClient arg )
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(
new[] {AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue()} );
state.Callback = new Uri( NativeApplicationClient.OutOfBandCallbackUrl );
if( !string.IsNullOrEmpty( refreshToken ) ) {
try {
state.RefreshToken = refreshToken;
arg.RefreshToken( state );
} catch {
refreshToken = null;
}
}
// If the refresh token is empty, request a new one by opening
// a browser window. Allows the user to paste its authorization token
// and saves the refresh token.
if( string.IsNullOrEmpty( refreshToken ) ) {
Uri authUri = arg.RequestUserAuthorization( state );
// Request authorization from the user (by opening a browser window):
Process.Start( authUri.ToString() );
Console.Write( " Authorization Code: " );
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
state = arg.ProcessUserAuthorization( authCode, state );
refreshToken = state.RefreshToken;
}
return state;
}