C# Google Calendar V3在本地环境之外使用时挂起
我正在为.net版本的Google日历API编写一个包装器。身份验证非常简单,并且在本地工作良好(localhost:port) 但是,问题是,当相同的代码部署到生产环境中时(当然,机密已更改),每当我尝试访问数据时,应用程序都会挂起。没有例外,没有错误代码,它只是一直在加载C# Google Calendar V3在本地环境之外使用时挂起,c#,asp.net,oauth,google-api,google-calendar-api,C#,Asp.net,Oauth,Google Api,Google Calendar Api,我正在为.net版本的Google日历API编写一个包装器。身份验证非常简单,并且在本地工作良好(localhost:port) 但是,问题是,当相同的代码部署到生产环境中时(当然,机密已更改),每当我尝试访问数据时,应用程序都会挂起。没有例外,没有错误代码,它只是一直在加载 有人经历过这种情况吗?我怀疑问题在于您使用的是AuthorizeAsync(…).Result。使用Result属性阻止当前线程。。。我怀疑ASP.NET试图在同一线程中安排继续(当身份验证完成时),从而导致死锁 考虑到您
有人经历过这种情况吗?我怀疑问题在于您使用的是
AuthorizeAsync(…).Result
。使用Result
属性阻止当前线程。。。我怀疑ASP.NET试图在同一线程中安排继续(当身份验证完成时),从而导致死锁
考虑到您仍在阻塞,是否有任何特定的原因需要使用AuthorizeAsync
调用?您通常会在异步方法的上下文中使用它,其中您有:
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(...);
(目前,我看不到同步替代方案,但这可能是您应该寻找的。我猜,GoogleWebAuthorizationBroker
不是设计用于这种情况的。)
诚然,关于ASP.NET同步上下文或Google auth API,我几乎不知道我应该知道多少,但希望解释为什么会出现问题可能是一个很好的起点。。。您可能也想阅读。我怀疑问题在于您使用的是
AuthorizeAsync(…)。Result
。使用Result
属性阻止当前线程。。。我怀疑ASP.NET试图在同一线程中安排继续(当身份验证完成时),从而导致死锁
考虑到您仍在阻塞,是否有任何特定的原因需要使用AuthorizeAsync
调用?您通常会在异步方法的上下文中使用它,其中您有:
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(...);
(目前,我看不到同步替代方案,但这可能是您应该寻找的。我猜,GoogleWebAuthorizationBroker
不是设计用于这种情况的。)
诚然,关于ASP.NET同步上下文或Google auth API,我几乎不知道我应该知道多少,但希望解释为什么会出现问题可能是一个很好的起点。。。你可能也想读书。我同意你的看法。不管出于什么原因,我也曾经历过。那是一座加略山。 下面的代码适用于我。这是我自己修订的Google API简单任务ASP.NET示例 添加这些用法
using System.IO;
using System.Threading;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.Util.Store;
这些
CalendarService service;
static string gFolder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage");
protected void Page_Load(object sender, EventArgs e)
{
IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = GetClientConfiguration().Secrets,
DataStore = new FileDataStore(gFolder),
Scopes = new[] { CalendarService.Scope.Calendar }
});
var uri = Request.Url.ToString();
var code = Request["code"];
if (code != null)
{
var token = flow.ExchangeCodeForTokenAsync(UserId, code,
uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;
// Extract the right state.
var oauthState = AuthWebUtility.ExtracRedirectFromState(
flow.DataStore, UserId, Request["state"]).Result;
Response.Redirect(oauthState);
}
else
{
var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId,
CancellationToken.None).Result;
if (result.RedirectUri != null)
{
// Redirect the user to the authorization server.
Response.Redirect(result.RedirectUri);
}
else
{
// The data store contains the user credential, so the user has been already authenticated.
service = new CalendarService(new BaseClientService.Initializer
{
ApplicationName = "Calendar API Sample",
HttpClientInitializer = result.Credential
});
}
}
}
public static GoogleClientSecrets GetClientConfiguration()
{
using (var stream = new FileStream(gFolder + @"\client_secrets.json", FileMode.Open, FileAccess.Read))
{
return GoogleClientSecrets.Load(stream);
}
}
希望能有帮助。我和你在一起。不管出于什么原因,我也曾经历过。那是一座加略山。 下面的代码适用于我。这是我自己修订的Google API简单任务ASP.NET示例 添加这些用法
using System.IO;
using System.Threading;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.Util.Store;
这些
CalendarService service;
static string gFolder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage");
protected void Page_Load(object sender, EventArgs e)
{
IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = GetClientConfiguration().Secrets,
DataStore = new FileDataStore(gFolder),
Scopes = new[] { CalendarService.Scope.Calendar }
});
var uri = Request.Url.ToString();
var code = Request["code"];
if (code != null)
{
var token = flow.ExchangeCodeForTokenAsync(UserId, code,
uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;
// Extract the right state.
var oauthState = AuthWebUtility.ExtracRedirectFromState(
flow.DataStore, UserId, Request["state"]).Result;
Response.Redirect(oauthState);
}
else
{
var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId,
CancellationToken.None).Result;
if (result.RedirectUri != null)
{
// Redirect the user to the authorization server.
Response.Redirect(result.RedirectUri);
}
else
{
// The data store contains the user credential, so the user has been already authenticated.
service = new CalendarService(new BaseClientService.Initializer
{
ApplicationName = "Calendar API Sample",
HttpClientInitializer = result.Credential
});
}
}
}
public static GoogleClientSecrets GetClientConfiguration()
{
using (var stream = new FileStream(gFolder + @"\client_secrets.json", FileMode.Open, FileAccess.Read))
{
return GoogleClientSecrets.Load(stream);
}
}
希望能有所帮助。首先:
一步一步
1) 在控制器中创建方法:
public async Task<ActionResult> Test()
{
//your code
}
3) 配置谷歌控制台首先:
一步一步
1) 在控制器中创建方法:
public async Task<ActionResult> Test()
{
//your code
}
3) 配置谷歌控制台它显示重定向Uri未匹配。如何解决这个问题?它说重定向Uri未匹配。如何解决这个问题?