Reputation: 193
i am using google calendar api service to access user calendar, and it is working fine for me in my local, but it is not working in server below is the code which is working fine in my local.
public ActionResult AddGoogleEvent()
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = clientid,
ClientSecret = clientsecret,
},
new[] { CalendarService.Scope.Calendar },
"user",
CancellationToken.None).Result;
var service = new CalendarService(new BaseClientService.Initializer(){
HttpClientInitializer = credential,
ApplicationName = "sampleappilication"
});
Google.Apis.Calendar.v3.Data.Event event1 = new Google.Apis.Calendar.v3.Data.Event()
{
Summary = "Appointment",
Location = location,
Start = new Google.Apis.Calendar.v3.Data.EventDateTime()
{
DateTime = new DateTime(Convert.ToInt32(yy), Convert.ToInt32(mn), Convert.ToInt32(dy), Convert.ToInt32(sthour), Convert.ToInt32(stminute), 0),
TimeZone = location
},
End = new Google.Apis.Calendar.v3.Data.EventDateTime()
{
DateTime = new DateTime(Convert.ToInt32(yy), Convert.ToInt32(mn), Convert.ToInt32(dy), Convert.ToInt32(ethour), Convert.ToInt32(etminute), 0),
TimeZone = "America/Los_Angeles"
},
Recurrence = new String[] {"RRULE:FREQ=WEEKLY;BYDAY=MO"},
Attendees = new List<Google.Apis.Calendar.v3.Data.EventAttendee>()
{
new Google.Apis.Calendar.v3.Data.EventAttendee() { Email = attendencess }
}
};
Google.Apis.Calendar.v3.Data.Event thisevent = service.Events.Insert(event1, "primary").Execute(); // Another error. "Does not contain a definition for Fetch"
string newEventID = thisevent.Id;
Session["Accepted"] = "Accepted";
return RedirectToAction("Eventconfirm");
}
Exception
Access to the path 'Google.Apis.Auth' is denied. StackTrace:at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Tasktask)atMicrosoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__1.MoveNext() in C:\Users\mdril\Documents\GitHub\google-api-dotnet-client\Src\GoogleApis.Auth.DotNet4\OAuth2\GoogleWebAuthorizationBroker.cs:line 59 Source: Microsoft.Threading.Tasks TargetSite: Void ThrowForNonSuccess(System.Threading.Tasks.Task)
Can any one suggest how to fix this
Upvotes: 1
Views: 5048
Reputation: 71
GoogleWebAuthorizationBroker.AuthorizeAsync works in a console app (or locally in the dev machine) but for a web app you need a web flow.
Please check this links:
http://www.c-sharpcorner.com/article/implementing-oauth2-0-authorization-for-google-in-asp-net/
https://stackoverflow.com/a/46604699/6080079
the first one is to get an accessToken and the second one is to get a service from that token.
Hope this help!
Upvotes: 1
Reputation: 117271
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = clientid,
ClientSecret = clientsecret,
},
new[] { CalendarService.Scope.Calendar },
"user",
CancellationToken.None).Result;
By default that code uses FileDataStore to store your credentials. FileDataStore will by default store the credentials in %appData%. My tutorial on filedatastore can be found here
Access to the path 'Google.Apis.Auth' is denied
Probably means that your server doesn't have access to write to that path. You can do something like this to change the path that the credentials are stored.
UserCredential credential;
using (var stream = new FileStream(clientSecretsJsonFilePath
,FileMode.Open
,FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { CalendarService.Scope.Calendar },
"LookIAmAUniqueUser",
CancellationToken.None,
new FileDataStore(@"c:\datastore",true)
).Result;
}
Alternatively you can also create your own implementation of idatastore and store the credentials how ever you like
Upvotes: 1