google calendar syncronization

I'm trying to syncronize my blazor server Net 8 application with google calendar to load all events and convert them in a syncfusion calendar where I can add more fields and information

I started from your description here

https://www.syncfusion.com/blogs/post/synchronize-google-calendar-with-syncfusion-blazor-scheduler

then I used this project from GitHub as starting demo

GitHub - SyncfusionExamples/google-calendar-synchronization-with-blazor-scheduler: Google calendar synchronization with Blazor Scheduler

I also updated the libraries to work with at least net core 6 (instead of 3.1 of the original demo)

I suppose I well configured the Google Cloud settings to create the credentials.json file because when I started this demo from my PC inside VisualStudio it works fine and I can show in your calendar my Google calendar events.

But when I published the demo inside a shared hosting where I usually work, the application stucks on this line of code without any error message in background 

Service = new CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName });

inside the GoogleService class

The web page stucks and I can't click anywhere. I can only refresh the page to go to other pages.

I noticed that the token from Google is saved inside a folder named token.json so I created this folder inside the shared hosting disk but no files are created inside from google and this folder has write privileges to everyone

Do you know what could be the problem ?



7 Replies

AK Ashokkumar Karuppasamy Syncfusion Team January 17, 2025 12:40 PM UTC

Hi Walter Martin,

We suspect that you are hosting on the IIS server. To resolve the issue you're facing with Google Calendar synchronization in the published application on IIS, here are a few possible solutions you can try:

1. File Permission Issues:

The issue might be related to file permissions when trying to write to the token.json file on the local IIS server. In IIS, the application pool identity (e.g., IIS AppPool\YourAppPoolName) might not have write permissions to the directory.

Solution:

  • Navigate to the folder where token.json is saved.
  • Right-click on the folder and select Properties.
  • Go to the Security tab and click Edit.
  • Ensure that the IIS AppPool\YourAppPoolName (or the user running the IIS app pool) has Write permissions.
  • Click Apply and OK.

2. Check Application Pool Identity:

Ensure that the application pool identity has the correct permissions to access files and directories. The default identity is often set to ApplicationPoolIdentity, which might not have sufficient rights.

Solution:

  • Open IIS Manager.
  • Find your application under Sites and click on it.
  • In the Actions pane, click on Advanced Settings.
  • Under Process Model, check the Identity property.
  • You can change it to a custom account that has full access to the folder containing the token.json.

3. Relative Path Issues:

When running in IIS, the relative paths may not work as expected, particularly if the application is running from a different location than when executed directly in Visual Studio.

Solution:

  • Ensure you are using absolute paths to save the token.json file. You can get the root path using the HostingEnvironment.MapPath or similar methods in your Blazor application.
  • Example: var tokenPath = Path.Combine(Directory.GetCurrentDirectory(), "token.json");

4. Check for Errors in IIS Logs:

IIS might be blocking or throwing errors that prevent saving the token. Check the IIS logs or the application logs for any detailed errors.

Solution:

  • Open Event Viewer and check for errors under Windows Logs > Application.
  • Look for any errors related to file access, permissions, or missing files.


Furthermore, if there is a possibility that we have misunderstood your requirement, we would greatly appreciate it if you could provide us with additional information on this. This will help us align our understanding with your expectations and provide you with the best possible assistance.

To better assist you, could you please provide the following:

  1. The steps you followed to deploy the project: On which server are you hosting the application?
  2. A sample or the relevant code how you integrate the Google Calendar synchronization in your Blazor Scheduler.
  3. Any error logs or console outputs from the environment.

This will help us diagnose the issue more effectively and provide a more accurate solution.


Let us know, and we’ll be happy to assist further!


Regards,

Ashok



WM Walter Martin January 17, 2025 03:17 PM UTC

First of all many thanks for these informations

I'm hosted in IIS server in a hosting environment so I don't have a direct access to IIS configuration

I can ask for any error log to the hosting provider.

This is what I did after reading your suggestion:

1) I set in the hosting panel the "full control" to "everyone" in the "www" folder

2) Since I don't find in the Google documentation if the folders and files must be created with relative or absolute path, I saved their "credentials.json" file inside "www" and also into "wwwroot" folders


As you can see in the relevent piece of code below in bold, Google let me choose the folder name where Google will write inside his token

I have chosen "gtoken" as folder name and thanks to the steps 1 and 2 above, I can see now that the folder "gtoken" is created in the hosting environment but unfortunately without any token file inside. In my personal PC, running the application in Visual Studio, the folder "gtoken" is either created and google write inside his token successfully


for this reason I think now the application can't complete the line in bold below because it's not able to write the token inside "gtolen" folder and I don't know why since "gtolen" folder is created under "www" folder where everyone has "full control" permission


            UserCredential credential;

            using (FileStream stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))

            {

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromStream(stream).Secrets, Scopes, "fzuser", CancellationToken.None, new FileDataStore("gtoken", true)).Result;

            }

            try

            {

                Service = new CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName });

            }

            catch (Exception ex)

            {

                ElmahCore.ElmahExtensions.RaiseError(ex);

            }



WM Walter Martin January 20, 2025 03:36 PM UTC

I can I add in attachment a sample code that is not exactly my project but it's a sample I found on the web to syncronize the Google Calendar with your Scheduler

I removed from the project only the token generated by google when the application runs from Visualsudio inside the token.json folder and the file credentials.json file from the root folder with my data

This application doesn't generate the google's token when running on a Shared Hosting where I can't access to IIS log instead, If I run it on my PC from VisualStudio everything works



Attachment: .github_3873bc28.zip


WM Walter Martin January 22, 2025 10:12 PM UTC

Is there any other suggestion on this ? it's still not working...



WM Walter Martin January 23, 2025 02:06 PM UTC

when I call in my app from Visual studio che function to get the Google calendar events, the line of code below, calls this page 

https://accounts.google.com/o/oauth2/auth?client_id={client_id}&response_type=token&redirect_uri={redirect_uri}&scope={scope}

in a new browser sheet and shows me which google account I want to use. After selectiing my account, my application starts to read the google calendar events.

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromStream(stream).Secrets, Scopes, "fzuser", CancellationToken.None, new FileDataStore("gtoken", true)).Result;

When I call the same function in the same app published in a shared server, this line of code simply freezes, without any error message in console and it doesn't show me this page

https://accounts.google.com/o/oauth2/auth?client_id={client_id}&response_type=token&redirect_uri={redirect_uri}&scope={scope}

The folder I decided to use: "gtoken" is created correctly inside the sared hosting environment so the web user has rights to write on the server but inside the "gtoken" folder it doesn't create the token file. What could prevent my web app to open another web page ?




AK Ashokkumar Karuppasamy Syncfusion Team January 23, 2025 02:11 PM UTC

Hi Walter Martin

We are currently validating this issue and will need until January 27, 2025, to complete the validation process. Thank you for your continued support; we will update you by January 27, 2025. We apologize for any inconvenience caused and appreciate your understanding.


Regards,
Ashok



SR Swathi Ravi Syncfusion Team January 31, 2025 12:56 PM UTC

Hi Walter Martin,

Sorry for the delay and thanks for your patience.

The cause of the issue is that token.jon file was not generated on the publishing. When publishing a Blazor application, the token.json file is not included by default because .json files are typically not marked for publishing. To ensure that the token.json file is included in the published output, follow these steps:

Solution: Modify the .csproj File

Add the following <ItemGroup> entry to your Blazor project’s .csproj file:

<ItemGroup>
  <Content Include="token.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  </Content>
</ItemGroup>

This configuration ensures that token.json is copied to both the build output and the published directory. And ensure that token.json is placed in the root folder of your project or adjust the file path in the Include attribute accordingly.

Alternative Solution:

You can directly use the AuthorizeAsync() method to handle the token securely without needing a file. This approach eliminates the need to store sensitive information in a local file and instead handles authorization directly in the code.

Below is current implementation:


Try our shared solution and get back to us if you need any further assistance. We are here to help.

Regards,
Swathi

Loader.
Up arrow icon