Search code examples
gogoogle-apigoogle-calendar-apigoogle-oauth

Credential to connect the Google Calendar API in Go


I am converting a PHP application to access Google calendar to Go. I used this step by step to get started.

All went smoothly, but when I run quickstart.go, I get the following error:

Unable to parse client secret file to config: oauth2/google: missing redirect URL in the client_credentials.json exit status 1

Content of the client_secret.json is:

{  
   "installed":{  
      "client_id":"***********content.com",
      "project_id":"*******",
      "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"
   }
}

That client_secret.json file is located at the root of my GOPATH as instructed in the step by step

I already have aOAuth 2.0 client ID for my PHP app that works just fine in PHP. I just want to use that one in the a new Go application to access multiple user calendars, but when I download the json file attached to that ID, I am getting the error above. Maybe the quickstart.go is not meant for that usage.

Any hints?


Solution

  • When you create OAuth credentials at https://console.developers.google.com/apis/credentials the dialog initially prompts you to "Configure your OAuth client" and you can choose between "Web application", "Desktop app", etc.

    The client.json obtained for the generated OAuth credentials may not contain a "Return URL", depending on the type chosen initially.

    For example, for "Web application" the client.json does not have a redirect URL:

    {
      "web": {
        "client_id": "x.apps.googleusercontent.com",
        "project_id": "x",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_secret": "x"
      }
    }
    

    While for a "Desktop app" it has:

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

    The Go oauth.google module always requires a return URI: https://github.com/golang/oauth2/blob/0f29369cfe4552d0e4bcddc57cc75f4d7e672a33/google/google.go#L61