Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Data to Google Sheets using Java

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": "[email protected]",
    "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.

So I use this 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.

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

like image 962
nkhl Avatar asked Feb 11 '26 10:02

nkhl


1 Answers

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

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.

like image 105
Martí Avatar answered Feb 13 '26 22:02

Martí



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!