使用Java将数据写入Google表格 [英] Writing Data to Google Sheets using Java

查看:73
本文介绍了使用Java将数据写入Google表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将收到的所有反馈信息放入Google表格中.我正在按照此文章进行此操作,但问题是如果我遵循每当需要将某些内容添加到Google工作表时,我都必须手动允许该项目访问我的文章.我了解我可以使用Google服务帐户来绕过手动验证,但我不能.我与服务帐户共享了Google表格,并使用了服务帐户的json并收到了错误.

I need to put all the feedbacks that I am receiving in a Google Sheet. 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. I understand that I can use google service account to bypass the manual verification but I am not able to. I shared the Google Sheet with the service account and used service account's json and got an error.

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-> ;,我仍然收到此错误.凭证->授权的重定向URI .

所以我用这个杰森

{
  "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表格进行交互的权限.

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.

请提出我的建议.任何帮助将不胜感激.

Please suggest what I can do. Any help will be appreciated.

推荐答案

如果还没有,应该阅读 Sheets API的Java快速入门.

If you haven't yet, you should read the Java quickstart for the Sheets API before anything else.

之后,最好的去处可能是服务帐户文章,说明什么是服务帐户;然后是使用服务帐户进行服务器到服务器通信的官方指南,其中有说明和代码(Java是其中的一种语言).还有 OAuth 2.0的Java库指南可以帮助您进一步了解图书馆.

After that, the best place to go is probably the Service Accounts article, which explains what are Service Accounts; 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). There is also the Java library guide to OAuth 2.0 which may help you understand the library more.

这篇关于使用Java将数据写入Google表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆