Setting up authentication for Google Calendar API in Golang application

I’m working on migrating my existing PHP project that uses Google Calendar API to a new Go implementation. I followed the official Google documentation to set up the basic structure.

package main

import (
    "fmt"
    "google.golang.org/api/calendar/v3"
    "golang.org/x/oauth2/google"
)

func main() {
    credentialsFile := "credentials.json"
    // Error occurs here when trying to load config
    config, err := google.ConfigFromJSON(data, calendar.CalendarScope)
    if err != nil {
        fmt.Printf("Error loading credentials: %v", err)
    }
}

When I execute my program, I encounter this error message:

Cannot parse credentials file: missing redirect URL in the oauth_credentials.json

My credentials file structure looks like this:

{
  "installed": {
    "client_id": "my-client-id.googleusercontent.com",
    "project_id": "my-project-name",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs"
  }
}

I have an existing OAuth client that works perfectly with my PHP application. I want to reuse the same credentials for my Go version to access user calendar data. However, downloading the JSON file from Google Console gives me the error mentioned above.

What could be causing this authentication issue?

I hit this exact problem converting a Laravel app to Go. Your PHP OAuth client config is missing the redirect_uris field that Go’s OAuth2 needs. Don’t bother creating new desktop credentials - just update your existing web app credentials in Google Console. Go to your OAuth 2.0 client settings and add “http://localhost:8080” and “urn:ietf:wg:oauth:2.0:oob” to the authorized redirect URIs. Download the updated JSON file. Now you can use the same client ID for both apps. You’ll still need to fix that undefined data variable though - use ioutil.ReadFile to read the credentials file before passing it to ConfigFromJSON.

check your oauth client setup in google console - you probably created it as ‘web application’ instead of ‘desktop app’. that’s why redirect_uris is missing. also, your code has a data variable that isn’t defined anywhere. you need to read the file first with ioutil.ReadFile.

yeah totally! switchin to desktop app is key here. the oauth client type is super important since web apps and desktop apps have different redirect URIs. go is stricter on this. just recreate the oauth client in google console for desktop and download the new json. should work after that!

Google Calendar API auth issues are the worst when you’re on deadline. Been there - watched this exact problem kill deployments.

Everyone’s throwing manual fixes at you, but there’s a smarter way. Skip the OAuth headaches and automate the whole integration.

Had a migration project last month syncing calendar data across multiple systems. OAuth setup, token refresh, different credential types - total time sink.

Built it as an automated workflow instead. No credential files, no OAuth debugging, no manual token management. The platform handles Google API auth automatically.

You can trigger on calendar events, sync with your Go app, handle complex calendar logic. 30 minutes setup vs days debugging OAuth.

Using this for all our Google API stuff now. Way more reliable than managing credentials across environments.

Check out the automation approach: https://latenode.com

Hit this exact problem switching from PHP to Go for calendar stuff last year. You’ve got two issues - missing redirect_uris AND a variable mismatch.

You’re declaring credentialsFile but passing undefined data to ConfigFromJSON. Here’s the fix:

package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "google.golang.org/api/calendar/v3"
    "golang.org/x/oauth2/google"
)

func main() {
    credentialsFile := "credentials.json"
    data, err := ioutil.ReadFile(credentialsFile)
    if err != nil {
        fmt.Printf("Error reading file: %v", err)
        return
    }
    
    config, err := google.ConfigFromJSON(data, calendar.CalendarScope)
    if err != nil {
        fmt.Printf("Error loading credentials: %v", err)
        return
    }
}

Don’t reuse your PHP web app credentials. Create a fresh OAuth 2.0 Client ID in Google Console and pick “Desktop application”.

Desktop credentials come with client_secret and proper redirect URIs built-in. Way cleaner than hacking web app credentials.

This is a credential type mismatch between platforms. I faced a similar issue while migrating from PHP to Go - Google’s OAuth in Go validates redirect URIs much more strictly than other languages. Your PHP credentials are likely set up as a web application client, which lacks the redirect_uris array that desktop apps require. It’s best not to alter your existing setup. Instead, create separate OAuth credentials specifically for your Go app. In Google Cloud Console, select ‘Desktop application’ when generating new credentials; this will automatically generate the redirect_uris field with “urn:ietf:wg:oauth:2.0:oob” and “http://localhost”. Additionally, ensure you are passing a well-defined data variable to ConfigFromJSON; make sure to read the credentials file before parsing it.

Been dealing with Google API migrations for years and this one trips up everyone coming from PHP.

Go’s oauth2 library does strict validation on credentials structure. PHP doesn’t care about missing fields.

Two ways to fix this:

  1. Quick fix: Add redirect_uris manually to your existing JSON:
{
  "installed": {
    "client_id": "my-client-id.googleusercontent.com",
    "client_secret": "your-client-secret",
    "project_id": "my-project-name",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"]
  }
}
  1. Proper fix: Download fresh desktop app credentials from Console.

Also fix your variable issue:

data, err := ioutil.ReadFile(credentialsFile)
if err != nil {
    return err
}

I usually go with option 1 when migrating existing projects. Keeps everything consistent and saves time updating documentation.

Your credentials file is missing the redirect_uris field, which disrupts the OAuth2 flow in Go. To resolve this, download credentials for a ‘Desktop application’ from the Google Cloud Console, as this includes the necessary redirect URIs.

Your credentials should look like this:

{
  "installed": {
    "client_id": "your-client-id.googleusercontent.com",
    "client_secret": "your-client-secret",
    "project_id": "your-project-name",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob","http://localhost"]
  }
}

Also, ensure you read the credentials file correctly before passing data to ConfigFromJSON by using ioutil.ReadFile.