1

Hei
I'm having trouble keeping my google OAuth Refresh Token valid for a small application I'm writing. I need to get data from a spreadsheet to a server / desktop application. I'm trying to authorize with OAuth, which works for a week, then stops.
According to this post, this is expected behaviour:
https://stackoverflow.com/a/67966982/16509954
Another answer in the same thread posts a method how to permanently give access and not get your token expired: https://stackoverflow.com/a/66292541/16509954
I did this but my token still keeps expiring. Any ideas what I'm doing wrong?

I'm using the python library, my code is pretty much identical to the example given in the documentation quickstart.py:
https://developers.google.com/sheets/api/quickstart/python

the_strange
  • 296
  • 2
  • 10

1 Answers1

1

Refresh tokens can expire for a number of reasons the main one these days being that your application is still in the testing phase.

enter image description here

Set your application over to production in Google cloud console and have it verified and the refresh tokens will not expire after a week.

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


SCOPES = ['https://www.googleapis.com/auth/drive.drive']
KEY_FILE_LOCATION = '<REPLACE_WITH_JSON_FILE>'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'


def initialize_sheets():
  """Initializes an sheets service object.

  Returns:
    An authorized sheets  service object.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

  # Build the service object.
  service = build('sheets', 'v4', credentials=creds)

  return service 
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    Thanks, but this is an App only I will be using. Verification would require the following things: 1. An official link to your app's Privacy Policy 2. A YouTube video showing how you plan to use the Google user data you get from scopes 3. A written explanation telling Google why you need access to sensitive and/or restricted user data 4. All your domains verified in Google Search Console This isn't really feasible for private purposes. – the_strange Jul 23 '21 at 12:05
  • 1
    Private or public purpose this is how things work now. If you want a refresh token that will last longer then 7 days you will need to set it in production and go though the verification process. Otherwise you will have a 7 day refresh token. There are no other options for you. This change has effected a lot of developer with single use scripts like yours. We are stuck until google gives us another option. – Linda Lawton - DaImTo Jul 23 '21 at 12:13
  • 1
    What about using a Service Account? Would that work or be subject to the same restrictions? https://developers.google.com/identity/protocols/oauth2/service-account – the_strange Jul 23 '21 at 12:15
  • 1
    Yes service accounts are supported by the google sheets api. They dont need to be verified – Linda Lawton - DaImTo Jul 23 '21 at 12:20
  • Check edit that might get you started. You only need to change your auth all your other code should work fine. Code is untested. – Linda Lawton - DaImTo Jul 23 '21 at 12:24