在 Go 中连接 Google Calendar API 的凭据

rip*_*pat 6 google-calendar-api google-api go google-oauth

我正在将用于访问 Google 日历的 PHP 应用程序转换为 Go。我是按照这个一步步开始的。

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

无法将客户端机密文件解析为配置:oauth2/google:client_credentials.json 退出状态 1 中缺少重定向 URL

其内容为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"
   }
}
Run Code Online (Sandbox Code Playgroud)

client_secret.json文件位于我的 GOPATH 的根目录,按照分步说明进行操作

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

有什么提示吗?

ale*_*leb 5

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

为生成的 OAuth 凭据获取的信息client.json可能不包含“返回 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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

{
  "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"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

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