locked
Google calendar - API sample RRS feed

  • Question

  • User-909867351 posted

    Hi

    I try to create one appoitment in google calendar . I use the following code:

    protected void Page_Load(object sender, EventArgs e)
        {
            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    new ClientSecrets
                    {
                        ClientId = "myid",
                        ClientSecret = "mysecret",
                    },
                    new[] { CalendarService.Scope.Calendar },
                    "user",
                    CancellationToken.None).Result;
    
            // Create the service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Calendar API Sample",
            });
            Event myEvent = new Event
            {
                Summary = "Appointment",
                Location = "Somewhere",
                Start = new EventDateTime()
                {
                    DateTime = new DateTime(2018, 7, 14, 10, 0, 0),
                    TimeZone = "America/Los_Angeles"
                },
                End = new EventDateTime()
                {
                    DateTime = new DateTime(2018, 7, 14, 10, 30, 0),
                    TimeZone = "America/Los_Angeles"
                },
                Recurrence = new String[] {
          "RRULE:FREQ=WEEKLY;BYDAY=MO"
      },
                Attendees = new List<EventAttendee>()
          {
            new EventAttendee() { Email = "marito.lopes@gmail.com" }
          }
            };
    
            Event recurringEvent = service.Events.Insert(myEvent, "primary").Execute();
        }

    I have 2 problems

    1. If I upload to my website I got the error: 

      can't access path 'C:\Windows\system32\config\systemprofile\AppData\Roaming\Google.Apis.Auth'.

    2. If I work in local mode every time I lunch my webpage in localhost I got one different address like this one: http://localhost:53781/professor/googlecalendar.aspx therefore I can't authorize it on my google console because I don't know wich port will be used

    Any help with this issues?

    Thank you

    Saturday, July 14, 2018 11:20 AM

Answers

  • User283571144 posted

    Hi mariolopes,

    As far as I know, by default the Google .Net client library uses FileDatastore() file datastore by default stores the credentials in %appData%. 

    The "%appData%"'s path which set on your server is the "C:\Windows\system32\config\systemprofile\AppData\Roaming\".

    If you don't want to use th default appdata path, I suggest you could set FileDataStore by yourself.

    More details, you could refer to below codes:

    string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
    
    var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "xxx4671204-9khdjsqifkj2amsji2jce37p8lfn0166.apps.googleusercontent.com",
                ClientSecret = "ZdsrCa-uwG3GmpVLTfYDli-S",
            },
            new[] { CalendarService.Scope.Calendar },
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;

    Notice: 

    Make sure the credPath is some place that you have access to write to. 

    Related article:

    http://www.daimto.com/google-net-filedatastore-demystified/ 

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2018 3:20 AM

All replies

  • User1992938117 posted

    If I upload to my website I got the error: 

    can't access path 'C:\Windows\system32\config\systemprofile\AppData\Roaming\Google.Apis.Auth'.

    Provide the access to app pool user or the account which w3wp process is running.

    If I work in local mode every time I lunch my webpage in localhost I got one different address like this one: http://localhost:53781/professor/googlecalendar.aspx therefore I can't authorize it on my google console because I don't know wich port will be used

    You can configure your website to run under local host, follow below steps:

    1) In Solution Explorer, right click the your project and click Properties.

    2) In the left tab, click Web.

    3) Select Local IIS, check out your Project Url (should be http://localhost\<app name>, then create Virtual Directory.

    I hope this will help you.

    Sunday, July 15, 2018 7:10 AM
  • User-909867351 posted

    Thank you for your help

    One question: I have my website on a internet host . I can access  httpdocs, and others, folders from my account not c:\windows\system32 ...

    It's possible to access c:\windows\system32... in my internet host?

    Inside my asp.net app In my Bin file I have the files, why the app needs c:\windows\system32...?

    <input type="checkbox" class="checkbox" name="listCheckbox[]" value="Google.Apis.Oauth2.v2.dll" />  Google.Apis.Oauth2.v2.dll 2018/07/14 14:21 17.5 KB <button type="button"> </button>
    <input type="checkbox" class="checkbox" name="listCheckbox[]" value="Google.Apis.Oauth2.v2.dll.refresh" />  Google.Apis.Oauth2.v2.dll.refresh 2018/07/14 14:21 158 B <button type="button"> </button>
    <input type="checkbox" class="checkbox" name="listCheckbox[]" value="Google.Apis.Oauth2.v2.pdb" />  Google.Apis.Oauth2.v2.pdb 2018/07/14 14:21 4.1 KB <button type="button"> </button>
    <input type="checkbox" class="checkbox" name="listCheckbox[]" value="Google.Apis.Oauth2.v2.xml" />  Google.Apis.Oauth2.v2.xml

    About the permissions

    in my vs2017 propertires  url I got the

    http://localhost:53781

    Ok, but when I run it I got the message 

    The redirect URI in the request, http://localhost:50147/authorize/, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/xxxxxx

    50147 is a random number, everytime I got a different number

    and my page opens on 

    http://localhost:53781/googlecalendar.aspx

    It doesn't make any sense to me.

    Sunday, July 15, 2018 10:13 PM
  • User283571144 posted

    Hi mariolopes,

    As far as I know, by default the Google .Net client library uses FileDatastore() file datastore by default stores the credentials in %appData%. 

    The "%appData%"'s path which set on your server is the "C:\Windows\system32\config\systemprofile\AppData\Roaming\".

    If you don't want to use th default appdata path, I suggest you could set FileDataStore by yourself.

    More details, you could refer to below codes:

    string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
    
    var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "xxx4671204-9khdjsqifkj2amsji2jce37p8lfn0166.apps.googleusercontent.com",
                ClientSecret = "ZdsrCa-uwG3GmpVLTfYDli-S",
            },
            new[] { CalendarService.Scope.Calendar },
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;

    Notice: 

    Make sure the credPath is some place that you have access to write to. 

    Related article:

    http://www.daimto.com/google-net-filedatastore-demystified/ 

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2018 3:20 AM