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

Evi*_*eer 0 c# asp.net oauth google-calendar-api google-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;
Run Code Online (Sandbox Code Playgroud)

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

有没有人经历过这个?

小智 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;
Run Code Online (Sandbox Code Playgroud)

还有这些...

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.