I'm struggling to get two-way synchronization between my org-mode calendar and Google Calendar to work. I'm using org-caldav. I created credentials (client ID and client secret) through Google that work with org-gcal, but org-caldav using the same credentials fails to authenticate against Google using OAuth2. I'm seeing the following behaviour:

  1. After pasting the OAuth response string from the browser, org-caldav asks me for a username and password for https://accounts.google.com/o/oauth2/token and I don't know what to enter here. I tried my Google password (but I have 2-factor authentication enabled), a specially created app password, and the client ID and secret, and none work. It looks like the POST request that gives access to the OAuth token needs separate authentication, but isn't the whole point of OAuth to stop the exchange of usernames and passwords over HTTP?
  2. I get lots of popup windows asking me for credentials for the encrypted plstore, even though plstore-cache-passphrase-for-symmetric-encryption is set to t. GPG_AGENT_INFO is unset, both in .emacs and using unset GPG_AGENT_INFO in bash. Nevertheless, Emacs uses the external GPG agent.
  3. After entering the passphraze dozens of times, nevertheless in the end I get a 401 error ("Unauthorized") and a message "Invalid token".

After some struggling, the relevant sections from my .emacs look like this:

(require 'oauth2)
(require 'org-caldav)

(setq org-caldav-debug-level 2)

(setq plstore-cache-passphrase-for-symmetric-encryption t)
;; Set environment variable in order to avoid being asked for the passphrase lots of times
(setenv "GPG_AGENT_INFO")

;; Actual calendar configuration edit this to meet your specific needs
(setq org-caldav-url 'google)
(setq org-caldav-oauth2-client-id "client-id-from-google")
(setq org-caldav-oauth2-client-secret "client-secret-from-google")
(setq org-caldav-calendars
  '((:calendar-id "my-google-username@googlemail.com"
     :files ("~/Dropbox/Org/inbox.org" "~/Dropbox/Org/personal.org")
     :inbox "~/Dropbox/Org/gcaldav.org")
    ))
(setq org-caldav-backup-file "~/Dropbox/Org/gcaldav-backup.org")
(setq org-caldav-save-directory "~/Dropbox/Org/")

(setq org-icalendar-alarm-time 1)
;; This makes sure to-do items as a category can show up on the calendar
(setq org-icalendar-include-todo t)
;; This ensures all org "deadlines" show up, and show up as due dates
(setq org-icalendar-use-deadline '(event-if-todo event-if-not-todo todo-due))
;; This ensures "scheduled" org items show up, and show up as start times
(setq org-icalendar-use-scheduled '(todo-start event-if-todo event-if-not-todo))

Here's the debug output from org-caldav that shows the failed authentication attempts.

========== Started sync.
Syncing first calendar entry:
(:calendar-id "my-google-username@googlemail.com" :files ("~/Dropbox/Org/inbox.org" "~/Dropbox/Org/personal.org") :inbox "~/Dropbox/Org/gcaldav.org")
Check connection for https://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/.
Got error while checking connection (will try again):
(error "Error while checking for OPTIONS at URL     httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/: 401 Unauthorized")
Check connection for httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/.
Got error while checking connection (will try again):
(error "Error while checking for OPTIONS at URL httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/: 401 Unauthorized")
Check connection for httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/.
Got error while checking connection (will try again):
(error "Error while checking for OPTIONS at URL httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/: 401 Unauthorized")
Check connection for httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/.
Got error while checking connection (will try again):
(error "Error while checking for OPTIONS at URL httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/: 401 Unauthorized")
Check connection for httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/.
Check connection for httрs://apidata.googleusercontent.com/caldav/v2/my-google-username@googlemail.com/events/.

What am I doing wrong?

New contributor
rxmd is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

rxmd is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.