在 Go 中连接 Google 日历 API 的凭据

Credential to connect the Google Calendar API in Go

我正在将 PHP 应用程序转换为访问 Google 日历到 Go。我用这个 step by step 开始。

一切顺利,但是当我运行 quickstart.go时,出现以下错误:

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

client_secret.json 的内容是:

{  
   "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"
   }
}

那个 client_secret.json 文件位于我的 GOPATH 的根目录下,按照步骤中的指示

我的 PHP 应用程序已经有一个 OAuth 2.0 client ID,它在 PHP 中运行良好。我只想在新的 Go 应用程序中使用它来访问多个用户日历,但是当我下载附加到该 ID 的 json 文件时,出现上述错误。也许 quickstart.go 不适合这种用法。

有什么提示吗?

当您在 https://console.developers.google.com/apis/credentials 创建 OAuth 凭据时,对话框最初会提示您“配置您的 OAuth 客户端”,您可以在“Web 应用程序”、“桌面应用程序”等之间进行选择。

为生成的 OAuth 凭据获取的 client.json 可能不包含“Return URL”,具体取决于最初选择的类型。

例如,对于“Web 应用程序”,client.json 没有重定向 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"
  }
}

对于“桌面应用程序”,它具有:

{
  "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"
    ]
  }
}

Go oauth.google 模块始终需要 return URI:https://github.com/golang/oauth2/blob/0f29369cfe4552d0e4bcddc57cc75f4d7e672a33/google/google.go#L61