Writing Data to Google Sheets using Java

Viewed 94

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}

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.

1 Answers
Related