【问题标题】:Writing Data to Google Sheets using Java使用 Java 将数据写入 Google 表格
【发布时间】:2021-04-06 12:26:31
【问题描述】:

我需要将收到的所有反馈都放在 Google 表格中。我正在关注article 来执行此操作,但问题是,如果我按照文章进行操作,则每次需要将某些内容添加到 Google 工作表时,我都必须手动允许访问我的项目。我知道我可以使用 google 服务帐户绕过手动验证,但我不能。我与服务帐户共享 Google 表格并使用服务帐户的 json 并收到错误。

JSON:

{
  "web": {
    "type": "service_account",
    "project_id": "demoproject",
    "private_key_id": "*********************",
    "private_key": "************************",
    "client_email": "writedata@demoproject.iam.gserviceaccount.com",
    "client_id": "107266171145536070455",
    "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_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/writedata%40demosheetproject.iam.gserviceaccount.com"
  }
}

Error:

The redirect URI in the request, http://localhost:50936/Callback, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

即使我已将 http://localhost/Callback 添加到 Google Cloud Console -> Credentials -> Authorized redirect URIs,我仍然会收到此错误。

所以我使用这个 Json

{
  "web":
   {
     "client_id":"**********.apps.googleusercontent.com",
     "project_id":"demoproject",
     "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":"***********",
     "redirect_uris":["http://localhost:61055"],
     "javascript_origins":["http://localhost:61055"]
   }
}

现在数据已填充到 Google 表格中,但该项目要求用户登录并授予其每次与 Google 表格交互的权限。

请建议我能做什么。任何帮助将不胜感激。

【问题讨论】:

    标签: java google-cloud-platform google-sheets-api


    【解决方案1】:

    如果您还没有阅读,请先阅读the Java quickstart for the Sheets API

    在那之后,最好去的地方可能是the Service Accounts article,它解释了什么是Service Accounts;其次是the official guide on server-to-server communication using service accounts,它有解释和代码(Java 是其中一种语言)。还有the Java library guide to OAuth 2.0可以帮助你更多地了解图书馆。

    【讨论】:

    • 好的,谢谢。我会浏览资源。
    • 嗨,我浏览了建议的文章,但仍然遇到同样的问题。你能帮我解决这个问题吗?
    • 你在什么时候卡住了?您是否能够通过服务帐户的凭据文件获取 GoogleCredential
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多