在本地环境之外使用时,Google Calendar V3会挂起

时间:2014-09-18 13:54:32

标签: c# asp.net oauth google-api google-calendar-api

我正在研究Google Calendar API的.net版本的包装器。身份验证非常简单,在本地工作正常(localhost:port)。

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
               new ClientSecrets
               {
                   ClientId = "The id of my public website",
                   ClientSecret = "The secret of my public website",
               },
               new[] { CalendarService.Scope.Calendar },
               "user",
               CancellationToken.None).Result;

            // Create the service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
            });

return service.Events.List("primary").Execute().Items;

但问题是,当相同的代码部署到生产环境时(当然,密码已更改),只要我尝试访问数据,应用程序就会挂起。没有例外,没有错误代码,它只是永远加载。

有没有人经历过这个?

3 个答案:

答案 0 :(得分:5)

我和你在一起。无论出于何种原因,这也发生在我身上。这是一个骷髅。 下面的代码适合我。这是我自己修订的Google API Simple Task 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 :(得分:0)

我怀疑问题是您正在使用AuthorizeAsync(...).Result。使用Result属性阻止当前线程...并且我怀疑 ASP.NET正在尝试在同一线程中安排继续(当身份验证完成时),从而导致死锁。

鉴于您无论如何都要阻止,您是否有任何特殊原因要使用AuthorizeAsync来电?您通常在异步方法的上下文中使用它,您将拥有:

var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(...);

(看GoogleWebAuthorizationBroker,我目前看不到同步替代品,但这应该是您应该寻找的。我的猜测是GoogleWebAuthorizationBroker不是设计用的在这种情况下。)

不可否认,我不太了解 ASP.NET同步上下文 Google auth API,但希望解释为什么会有这样的问题。问题可能是一个很好的起点......你可能也想阅读Stephen Cleary's article about synchronization contexts

答案 2 :(得分:0)

首先是:See Google MVC tutorial

一步一步

1)在控制器中创建方法:

 public async Task<ActionResult> Test()
    {
         //your code
    }

2)创建控制器:

public class AuthCallbackController : Google.Apis.Auth.OAuth2.Mvc.Controllers.AuthCallbackController
    {
        protected override Google.Apis.Auth.OAuth2.Mvc.FlowMetadata FlowData
        {
            get { return new AppFlowMetadata(); }
        }
    }

3)配置Google控制台

相关问题