var gFolder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage");
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "xxxxxxxxx.apps.googleusercontent.com",
ClientSecret = "xxxxxxxxxxxx0Kq9gwLdQwB1",
},
new[] { CalendarService.Scope.Calendar },
"user",
CancellationToken.None,
new FileDataStore(gFolder)).Result;
// Create the service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
The issue is, though, when the same code is deployed to production (secrets changed, of course), the application just hangs whenever I try to access the data. There's no exception, no error code, it just keeps loading forever.
Any advice?
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);
}
}