简体   繁体   English

使用 Java 将数据写入 Google 表格

[英]Writing Data to Google Sheets using Java

I need to put all the feedbacks that I am receiving in a Google Sheet.我需要将收到的所有反馈都放在 Google 表格中。 I was following this article to do this but the problem is that if I follow the article I have to manually allow access to my project every time there is something that needs to be added to the Google sheet.我一直在关注这篇文章来做到这一点,但问题是,如果我关注这篇文章,我必须在每次需要将某些内容添加到 Google 工作表时手动允许访问我的项目。 I understand that I can use google service account to bypass the manual verification but I am not able to.我知道我可以使用 google 服务帐户绕过手动验证,但我不能。 I shared the Google Sheet with the service account and used service account's json and got an error.我与服务帐户共享 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}

I keep getting this error even though I have added the http://localhost/Callback to Google Cloud Console -> Credentials -> Authorized redirect URIs .即使我已将http://localhost/Callback添加到Google Cloud Console -> Credentials -> Authorized redirect URIs ,我仍然会收到此错误。

So I use this Json所以我用这个 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"]
   }
}

and now data gets populated in the Google Sheet but the project asks the user to log in and give it permission to interact with Google Sheets everytime.现在数据已填充到 Google 表格中,但该项目要求用户登录并授予其每次与 Google 表格交互的权限。

Please suggest what I can do.请建议我能做什么。 Any help will be appreciated.任何帮助将不胜感激。

If you haven't yet, you should read the Java quickstart for the Sheets API before anything else.如果您还没有阅读其他内容,则应该先阅读表格 API 的 Java 快速入门

After that, the best place to go is probably the Service Accounts article , which explains what are Service Accounts;在那之后,go 最好的地方可能是服务帐户文章,它解释了什么是服务帐户; followed by the official guide on server-to-server communication using service accounts , which has an explanation and code (Java is one of the languages).其次是使用服务帐户进行服务器到服务器通信的官方指南,其中有解释和代码(Java 是其中一种语言)。 There is also the Java library guide to OAuth 2.0 which may help you understand the library more.还有Java 库指南 OAuth 2.0可以帮助您更多地了解该库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM