Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specific exception in case an authorization has expired #1169

Open
fredericomattos opened this issue Feb 11, 2023 · 3 comments
Open

Specific exception in case an authorization has expired #1169

fredericomattos opened this issue Feb 11, 2023 · 3 comments

Comments

@fredericomattos
Copy link

fredericomattos commented Feb 11, 2023

In order not to need to create a new authorization each time I need it (sometimes I use it 8 times per code loop), I create a global object:

import gspread

client_gspread = None
def send_sheet(id_sheet, page_sheet, sheet_data):
    global client_gspread
    if not client_gspread:
        client_gspread = gspread.service_account(filename='client_secret.json')

    sheet = client_gspread.open_by_key(id_sheet).worksheet(page_sheet)
    sheet.clear()
    sheet.update([sheet_data.columns.values.tolist()] + sheet_data.values.tolist())

The problem with this is that authorization has an expiration time, which we find in client_gspread.auth.expiry

It would be interesting if there was a single exception that handled the case of expired authorization, to be used directly when you need to update the authorization.

In the documentation in the area explaining how to authorization, it would be nice to also add how long an authorization lasts, so that new users can already create their projects knowing the time limitations of authorizations (putting the path to find the expiry time in the object created in the authorization, so there are no doubts between timeout or expiry). Oh sure, it would also be nice to add to the documentation the answer to the question "if I perform a real action using gspread, is the authorization expiration time updated or fixed?"

Because when the authorization is created, when analyzing its vars, we find the timeout attribute, which has no value, so the person may mistakenly understand that there is no expiration date.

@lavigne958 lavigne958 added Feature Request Need investigation This issue needs to be tested or investigated labels Feb 16, 2023
@lavigne958 lavigne958 removed the Need investigation This issue needs to be tested or investigated label May 15, 2023
@lavigne958
Copy link
Collaborator

Hi I had a look at the Credentials object, you're right it hold the expiry date, it's a date object so we could do something quiet nice with it.

We could add some feature that returns the expiry, raise some exception when it has expired (by checking the return code etc).
I found that the expiry only exists for oauth access, when using the service_account the property simply does not exists, so I need to find a way to make it work for both (no complicated just extra work).

I might make it fit for the next release, not 100% sure though.

lavigne958 added a commit that referenced this issue Apr 16, 2024
In gspread client add a new property expiry which returns the expiry
datetime object from the used credentials.

The expiry only exists for OAuth credentials otherwise it returns None.

closes #1169

Signed-off-by: Alexandre Lavigne <lavigne958@gmail.com>
@lavigne958
Copy link
Collaborator

@fredericomattos I added the extra property expiry in gspread client.

it returns the expiry for the current oauth credentials.

I tried to run some code with a few days between each run and every time I open a sheet, the token is being refreshed and the expiry time changes.

from there you can use this property to check the datetime and raise your Exception as you need.

Hope it solves your issue.

@lavigne958
Copy link
Collaborator

Hi @fredericomattos I ran some tests after many weeks.

I noticed the following behavior:

  • the expiry date is never in the future (it's always a data + time already in the past), so it does not provide any information
  • the expiry data + the flag expired in the credentials when using oauth is always refreshed (even after a long pause like a week without using the credentials).

I will go ahead with the PR exposing if the credentials are expired but I think its usage is limited. Just to let you know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants