问题
Running the code below never returns from the Execute function. I have a private calendar on my personal gmail account that I have shared with the developer.gserviceaccount.com account.
Looking at the API Manager, the usage/quotes show that I have used or even hit the api yet.
Any thoughts appreciated.
using System.Security.Cryptography.X509Certificates;
using System.Web;
using Google.Apis.Calendar.v3;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
public class GoogleLoginWithServiceAccount
{
public string getEventList()
{
var GoogleOAuth2CertificatePath = HttpContext.Current.Server.MapPath("~/xx/xx.p12");
var GoogleOAuth2EmailAddress = "xx-xxxx@developer.gserviceaccount.com";
var GoogleOAuth2PrivateKey = "notasecret";
var certificate = new X509Certificate2(GoogleOAuth2CertificatePath,
GoogleOAuth2PrivateKey, X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(GoogleOAuth2EmailAddress)
{
//User = GoogleAccount,
Scopes = new string[] { CalendarService.Scope.Calendar }
}.FromCertificate(certificate));
// Create the service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Testing"
});
var listRequest = service.CalendarList.List();
return listRequest.Execute().ToString();
}
}
I'm still running into this issue. I've created a simple app to reproduce this, anybody have any thoughts? dropbox.com/s/g45xcta7ii3hj51/GoogleTestWeb.zip?dl=0
回答1:
The problem is described more in details in https://github.com/google/google-api-dotnet-client/issues/606. It seems that we currently have a bug in 1.9.3, which is fixed in the github repository (The fix itself is in https://github.com/google/google-api-dotnet-client/pull/612).
A new release is planned to be in the next few weeks, so stay tuned; http://google-api-dotnet-client.blogspot.com/.
If meantime you want to play and test it yourself you can use the projects in GitHub with the Calendar Service. It's better for you to download the Calendar Service from https://developers.google.com/resources/api-libraries/download/calendar/v3/csharp to avoid binding redirects. I explained what you have to do in #606 itself.
Update (Dec 15th): New NuGet packages for 1.10.0 are available, read more about it at: http://google-api-dotnet-client.blogspot.com/2015/12/announcing-release-of-1100.html
回答2:
There is a difference between a calendar and calendarlist. In the Google Calendar website calendarlist appears on the left hand side its a list of calendars. However for a calendar to appear on calendarlist it has to be added to the calendarlist.
You gave your service account access to the calendar but did you programmatically tell it to add the calendar to its calendarlist? You should have used CalendarList: insert
I am not sure why you want to get the calendarlist. Personally I think you should just go with Calendars: get you already know the ID of the calendar from the google calendar website.
Tip: Execute is going to return an object I am not sure .ToString() on it is really the best idea.
update code:
/// <summary>
/// Returns metadata for a calendar.
/// Documentation:https://developers.google.com/google-apps/calendar/v3/reference/calendars/get
/// </summary>
/// <param name="service">Valid Autenticated Calendar service</param>
/// <param name="id">Calendar identifier.</param>
/// <returns>Calendar resorce: https://developers.google.com/google-apps/calendar/v3/reference/calendars#resource </returns>
public static Calendar get(CalendarService service, string id)
{
try
{
return service.Calendars.Get(id).Execute();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
code from My sample project on GitHub
来源:https://stackoverflow.com/questions/33618284/google-calendar-api-not-returning-from-execute-c-sharp