Google Calendar Sync Fails (GH Issue 8743)

https://github.com/twentyhq/twenty/issues/8743 Basically it's a scenario where the google integration works fine (as I can authenticate via google) and the auth screens present fine, but when google sends you back to the twenty server, the server flips out and crashes.
GitHub
When google calendar / auth is enabled, the authentication, authori...
Bug Description Google authentication for SSO and calendar sync is enabled on the server. The google client-id and client-secret are set, and google integration works. SSO login for users is functi...
37 Replies
shairozan
shairozanOP4w ago
@Raphaël Heya, figured I'd tag you here as mentioned So the initial problem I think I've solved. I had the AUTH_GOOGLE variables defined in the server, but not the worker. Defined them and re-deployed and the worker doesn't flip out now. Sync still shows failed, but not a lot of details as to why. Is there any information / documentation about how that whole process functions? When I try to reconnect I get:
(node:34) Warning: Label 'CalendarEventListFetchJob time' already exists for console.time()
(Use `node --trace-warnings ...` to show where the warning was created)
CalendarEventListFetchJob time: 58.645s
(node:34) Warning: Label 'CalendarEventListFetchJob time' already exists for console.time()
(Use `node --trace-warnings ...` to show where the warning was created)
CalendarEventListFetchJob time: 58.645s
shairozan
shairozanOP4w ago
Just get an immediate Sync Failed
No description
shairozan
shairozanOP4w ago
Running any of:
# from your worker container
yarn command:prod cron:messaging:messages-import
yarn command:prod cron:messaging:message-list-fetch
yarn command:prod cron:calendar:calendar-event-list-fetch
# from your worker container
yarn command:prod cron:messaging:messages-import
yarn command:prod cron:messaging:message-list-fetch
yarn command:prod cron:calendar:calendar-event-list-fetch
Just dumps a lot of Nest output, but no errors. So I'm kind of in the dark as to what's happening. Is there a way to turn up logging on the nest logger?
Raphaël
Raphaël4w ago
Normally we log errors inside the worker Could you send me the calendarChannel syncStatus and the messageChannel syncStatus and also their syncStage ? Do you see any events inside the db?
shairozan
shairozanOP4w ago
[Nest] 34 - 11/27/2024, 1:15:00 PM LOG [WorkspaceDatasourceFactory] Creating workspace data source for workspace bb9899bf-1318-41e5-b9ea-4adf3081cfb9 and metadata version 82
MessagingMessageListFetchCronJob time: 79.847ms
CalendarEventListFetchCronJob time: 2.67ms
MessagingMessagesImportCronJob time: 2.919ms
CalendarEventListFetchCronJob time: 27.639ms
[Nest] 34 - 11/27/2024, 1:15:00 PM LOG [WorkspaceDatasourceFactory] Creating workspace data source for workspace bb9899bf-1318-41e5-b9ea-4adf3081cfb9 and metadata version 82
MessagingMessageListFetchCronJob time: 79.847ms
CalendarEventListFetchCronJob time: 2.67ms
MessagingMessagesImportCronJob time: 2.919ms
CalendarEventListFetchCronJob time: 27.639ms
There's no errors in the worker, but the state is always failed. Not even pending. Have to check in the db. Where in the schema should I look?
Raphaël
Raphaël4w ago
Inside your workspace schema you will find calendarChannel and messageChannel
shairozan
shairozanOP4w ago
For message status it's "ONGOING" but for calendar status it's "FAILED_INSUFFICIENT_PERMISSIONS". Are there specifications for the OIDC requirements for calendar syncing anywhere? There may be something I missed in defining the google entry? That doesn't make a lot of sense since the consent screen explicitly lists the app asking for all of your calendars Well after a few minutes they're both FAILED_INSUFFICIENT_PERMISSIONS
[Nest] 34 - 11/27/2024, 3:00:00 PM LOG [MessagingMessageListFetchJob] Fetching full message list for workspace bb9899bf-1318-41e5-b9ea-4adf3081cfb9 and account 62d85fa8-4d76-4b7a-8a5d-54be2099c380
CalendarEventListFetchCronJob time: 16.597ms
MessagingMessagesImportCronJob time: 2.864ms
MessagingMessageListFetchJob time: 204.357ms
CalendarEventListFetchCronJob time: 24.321ms
MessagingMessagesImportCronJob time: 3.653ms
CalendarEventListFetchCronJob time: 23.068ms
MessagingMessagesImportCronJob time: 3.516ms
CalendarEventListFetchCronJob time: 24.153ms
MessagingMessagesImportCronJob time: 2.572ms
CalendarEventListFetchCronJob time: 23.938ms
MessagingMessagesImportCronJob time: 3.622ms
MessagingMessageListFetchCronJob time: 24.864ms
CalendarEventListFetchCronJob time: 2.631ms
MessagingMessagesImportCronJob time: 2.286ms
CalendarEventListFetchCronJob time: 25.663ms
MessagingMessagesImportCronJob time: 2.896ms
CalendarEventListFetchCronJob time: 24.213ms
MessagingMessagesImportCronJob time: 3.229ms
CalendarEventListFetchCronJob time: 24.262ms
MessagingMessagesImportCronJob time: 3.358ms
CalendarEventListFetchCronJob time: 24ms
MessagingMessagesImportCronJob time: 3.657ms
[Nest] 34 - 11/27/2024, 3:00:00 PM LOG [MessagingMessageListFetchJob] Fetching full message list for workspace bb9899bf-1318-41e5-b9ea-4adf3081cfb9 and account 62d85fa8-4d76-4b7a-8a5d-54be2099c380
CalendarEventListFetchCronJob time: 16.597ms
MessagingMessagesImportCronJob time: 2.864ms
MessagingMessageListFetchJob time: 204.357ms
CalendarEventListFetchCronJob time: 24.321ms
MessagingMessagesImportCronJob time: 3.653ms
CalendarEventListFetchCronJob time: 23.068ms
MessagingMessagesImportCronJob time: 3.516ms
CalendarEventListFetchCronJob time: 24.153ms
MessagingMessagesImportCronJob time: 2.572ms
CalendarEventListFetchCronJob time: 23.938ms
MessagingMessagesImportCronJob time: 3.622ms
MessagingMessageListFetchCronJob time: 24.864ms
CalendarEventListFetchCronJob time: 2.631ms
MessagingMessagesImportCronJob time: 2.286ms
CalendarEventListFetchCronJob time: 25.663ms
MessagingMessagesImportCronJob time: 2.896ms
CalendarEventListFetchCronJob time: 24.213ms
MessagingMessagesImportCronJob time: 3.229ms
CalendarEventListFetchCronJob time: 24.262ms
MessagingMessagesImportCronJob time: 3.358ms
CalendarEventListFetchCronJob time: 24ms
MessagingMessagesImportCronJob time: 3.657ms
There's also no errors in the worker that I can see They are exiting overly fast (messages specifically) I think I may have found it Well I thought. I was looking at the HAR of traffic to and from google to verify the scopes / claims requested since invalid_grant is one of the triggers I could find in the source code to set that state. I added https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/profile.emails.read To the sensitive scopes for the OIDC client, but no change afterwards. Was hoping that was it It may be a timing concern. I may try again later to see if the changes affect anything. Honestly if failures like that occur, some additional logging would be beneficial Even with debug mode on the worker and all logging levels enabled, I don't get any information. It just fails If it provides anything to assist, the worker configuration (redacted) is below:
NODE_VERSION=18.17.1
YARN_VERSION=1.22.19
REACT_APP_SERVER_BASE_URL=
SENTRY_RELEASE=
APP_SECRET=<REDACTED>
AUTH_GOOGLE_CLIENT_ID=<REDACTED>
AUTH_GOOGLE_CLIENT_SECRET=<REDACTED>
AUTH_GOOGLE_CALLBACK_URL=https://<REDACTED>/auth/google/redirect
FRONT_BASE_URL=<REDACTED>
PG_DATABASE_URL=<REDACTED>
ENABLE_DB_MIGRATIONS=false
CACHE_STORAGE_TYPE=redis
LOG_LEVELS=error,warn,log
AUTH_GOOGLE_ENABLED=true
SERVER_URL=https://crm.dev.metworx.com
CALENDAR_PROVIDER_GOOGLE_ENABLED=true
MESSAGING_PROVIDER_GMAIL_ENABLED=true
STORAGE_TYPE=local
MESSAGE_QUEUE_TYPE=bull-mq
REDIS_URL=redis://twentycrm-redis:6379
HOME=/home/node
NODE_VERSION=18.17.1
YARN_VERSION=1.22.19
REACT_APP_SERVER_BASE_URL=
SENTRY_RELEASE=
APP_SECRET=<REDACTED>
AUTH_GOOGLE_CLIENT_ID=<REDACTED>
AUTH_GOOGLE_CLIENT_SECRET=<REDACTED>
AUTH_GOOGLE_CALLBACK_URL=https://<REDACTED>/auth/google/redirect
FRONT_BASE_URL=<REDACTED>
PG_DATABASE_URL=<REDACTED>
ENABLE_DB_MIGRATIONS=false
CACHE_STORAGE_TYPE=redis
LOG_LEVELS=error,warn,log
AUTH_GOOGLE_ENABLED=true
SERVER_URL=https://crm.dev.metworx.com
CALENDAR_PROVIDER_GOOGLE_ENABLED=true
MESSAGING_PROVIDER_GMAIL_ENABLED=true
STORAGE_TYPE=local
MESSAGE_QUEUE_TYPE=bull-mq
REDIS_URL=redis://twentycrm-redis:6379
HOME=/home/node
Raphaël
Raphaël4w ago
Did you enable people api ? On the google cloud console We will need to improve this part indeed, we also have to update the documentation for self hosting for email an calendar connection. Sorry if it's a bit confusing!
Raphaël
Raphaël4w ago
No description
shairozan
shairozanOP3w ago
Yeah google people API is enabled I just silently get INSUFFICIENT PERMISSIONS for both calendar / gmail sync. So any additional details from the worker would be highly beneficial, and some clear guidance on requirements from google and its client. I may try to create a PR if I can figure out javascript to enable additional logging at the closures where the message gets processed
Raphaël
Raphaël3w ago
@charles
charles
charles3w ago
I think the most efficient would be to schedule a call with @shairozan and to compare the setup with the one we have in production We can also add a bit more log in this section before putting the channel in INSUFFICIENT_PERMISSIONS Last question @shairozan could you double check the scopes attached to your individual email acconts
charles
charles3w ago
Here is an example (sorry it's french)
No description
shairozan
shairozanOP3w ago
C'est bien. Yeah I can double check and provide the full details for the scopes / claims. I went through the source code to find the scope details for allocation.
shairozan
shairozanOP3w ago
May be some differences. I notice that yours lists: * Profile Data * Reading your Emails * Sending Emails * Managing Calendar
No description
shairozan
shairozanOP3w ago
For mine it's viewing email and settings but not processing / sending. Looks to be the only real difference I can see. It is worth pointing out that readonly for gmail is a restricted scope. That may have some affect in workspaces accounts for private / internal clients. That's something someone more familiar with google's odd permissions structure would have to comment on
shairozan
shairozanOP3w ago
Here are the scopes enabled for the OIDC client
No description
shairozan
shairozanOP3w ago
These were all derived from the OIDC authorization request. Capturing the traffic, the scopes requested were:
email
profile
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/calendar.events
https://www.googleapis.com/auth/profile.emails.read
email
profile
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/calendar.events
https://www.googleapis.com/auth/profile.emails.read
I don't think not having email sending is a problem based on the scopes here from an OIDC perspective. It may be that in the ingest process it expects email to be setup for outbound communications, and without that goes into insufficient permissions?
charles
charles3w ago
@Raphaël @martmull I don't see the scope to SEND emails ?
shairozan
shairozanOP3w ago
Is it potentially that the permissions expects the scope to send emails, but because I haven't configured anything for sending that it isn't getting expressed in OAuth Authorization request?
if (isGmailSendEmailScopeEnabled) {
scopes.push('https://www.googleapis.com/auth/gmail.send');
}
if (isGmailSendEmailScopeEnabled) {
scopes.push('https://www.googleapis.com/auth/gmail.send');
}
I'm going to add that to the client and see if it's getting requested at the least Nah. The requested scopes in the authorization from twenty still don't have email send. OIDC client allows it as a claim, just need to know what toggles isGmailSendEmailScopeEnabled Maybe that I'm missing a configuration item? Looks like it's handled in the core schema in postgres based on my provider. I found the features table and am pretty sure the key is IS_GMAIL_SEND_EMAIL_SCOPE_ENABLED, but the workspaceID value expects a UUID
Raph
Raph3w ago
I'm also interested in this. I haven't dug into it as of lately as it's not a critical thing for us at the moment, BUT the sync isn't as stable as I had hoped it would be.
shairozan
shairozanOP3w ago
Ah figured that part out. It's also in the core space
shairozan
shairozanOP3w ago
No description
shairozan
shairozanOP3w ago
Yeah you have to enable the feature in the core schema Now let's see if it flips out after auth Nah immediately goes into "Sync lost with mailbox". I'll give it some time as I have to get to a meeting
Raph
Raph3w ago
Ok, so mine is sort of the opposite. Calendar works flawlessly, and the email sync doesn't complete. I'm going to go back and dig into it all this week. I hope it all works out. I'll follow along.
shairozan
shairozanOP3w ago
Neither even starts for me. Both channels fall into INSUFFICIENT_PERMISSIONS so I haven't even started yet I'm at least getting closer to the ref from the above
email
profile
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/calendar.events
https://www.googleapis.com/auth/profile.emails.read
https://www.googleapis.com/auth/gmail.send
email
profile
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/calendar.events
https://www.googleapis.com/auth/profile.emails.read
https://www.googleapis.com/auth/gmail.send
Is what we're seeing in the auth request after enabling the feature
shairozan
shairozanOP3w ago
I forgot to mention that I'm available for a call to discuss about it, but my times are kind of crappy. You could book some time here
Raphaël
Raphaël3w ago
I don't see the AUTH_GOOGLE_APIS_CALLBACK_URL=http://<REDACTED>/auth/google-apis/get-access-token in your env vars
shairozan
shairozanOP3w ago
/app/packages/twenty-server $ env | grep CALLBACK
FRONT_AUTH_CALLBACK_URL=https://<REDACTED>/verify
AUTH_GOOGLE_CALLBACK_URL=https://<REDACTED>/auth/google/redirect
AUTH_GOOGLE_APIS_CALLBACK_URL=https://<REDACTED>/auth/google-apis/get-access-token
/app/packages/twenty-server $ env | grep CALLBACK
FRONT_AUTH_CALLBACK_URL=https://<REDACTED>/verify
AUTH_GOOGLE_CALLBACK_URL=https://<REDACTED>/auth/google/redirect
AUTH_GOOGLE_APIS_CALLBACK_URL=https://<REDACTED>/auth/google-apis/get-access-token
It's set Also if the callback URL wasn't set, we would have gotten to a network traffic issue. The attempt to come back from google after the authorization would have pointed at the default and failed. Wouldn't have gotten to the point of ingesting a token
shairozan
shairozanOP3w ago
So after adding the Gmail / Calendar APIs sync started, but it fails periodically with this in the worker logs
shairozan
shairozanOP3w ago
I can always create a separate issue in GH if we want to vs continuing to drag this one on It seems like it's something dealing with bigquery At least that's where I see it come up a lot. I don't see any references from the codebase to bigquery. It may be a scenario where its' a specific error that's not catalogued into a try / catch and is falling into a default throw within the message import It may be because I'm in a workspace with a private (internal) OIDC client. https://developers.google.com/identity/protocols/oauth2/production-readiness/google-workspace. We may be able to trust the app there. may have to investigate with my own personal workspace account
shairozan
shairozanOP3w ago
May be the case, but I'm not sure what qualifies as "high risk" from their perspective. If we're accessing attachments, it may be considered high risk
No description
shairozan
shairozanOP3w ago
Ok looks like you can specify in API Controls under settings to trust internal apps. That may get it to work. I'd have to work with our google admins to see if that resolves it, but I'm betting that's probably where it's flipping out
No description
Raphaël
Raphaël3w ago
I think this may solve it indeed Is your google app in test mode or is it published ?
shairozan
shairozanOP3w ago
It's not published anywhere but in the workspace To make it public means going through google verification, which takes money If it does, I'll make a PR for the documentation to explain requirements for an internal (workspace specific) OIDC client
Raphaël
Raphaël3w ago
It doesn't cost anything to go through google verification but it takes a little bit of time, but you don't have to publish it to make it work. It's just that users that aren't test users will receive a warning while trying to connect to the app.
shairozan
shairozanOP3w ago
Our use case is purely internal so an internal OIDC client inside of the workspaces makes more sense. The users see it like a typical google auth interaction and we don't have to worry about it ever being seen outside of the workspace. Thanks for the info though since I've traditionally shied away from public google OIDC integrations
Want results from more Discord servers?
Add your server