GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Client's auth token times out (StackOverflow) · Issue #1917 · googleapis/google-cloud-python

Client's auth token times out (StackOverflow) #1917

New issue
Copy link
New issue
Copy link
Closed
Closed
Client's auth token times out (StackOverflow)#1917
Copy link
Assignees
dhermes
Labels
api: bigtableIssues related to the Bigtable API.auth

Description

@tseaver
tseaver
opened on Jun 27, 2016
Issue body actions

@dhermes Seems like you have the best grasp on our oauthclient story.

http://stackoverflow.com/questions/37982336/unauthenticated-error-using-gcloud-python-to-connect-to-google-bigtable

/cc @sduskis

Activity

  1. tseaver
    added
    auth
    api: bigtableIssues related to the Bigtable API.
    on Jun 27, 2016
  2. tseaver
    assigned
    dhermes
    on Jun 27, 2016
  3. dhermes commented on Jun 29, 2016

    @dhermes
    dhermes
    on Jun 29, 2016
    Contributor
    More actions

    Ouch. Seems we need to turn it into a 401 so oauth2client can natively handle the NetworkError. I'll try to replicate.

  4. sduskis commented on Jul 6, 2016

    @sduskis
    sduskis
    on Jul 6, 2016
    Contributor
    More actions

    ping. What's that status?

  5. dhermes commented on Jul 18, 2016

    @dhermes
    dhermes
    on Jul 18, 2016
    Contributor
    More actions

    Sorry for delay (had a baby). The entire behavior of oauth2client is based on catching a 401 status, but gRPC doesn't do HTTP status codes. But we're using get_access_token() which does a refresh if the token is expired.

    I'm not sure how to replicate. It seems that a gRPC unauthenticated error could only occur if now >= self.token_expiry is false (when checking if the token is expired) just moments before the request but true during the request.

    But it's possible I misunderstand how the Python gRPC stubs are using the token (maybe they cache it?). I'm going to do some checking and report back.

  6. dhermes commented on Jul 18, 2016

    @dhermes
    dhermes
    on Jul 18, 2016
    Contributor
    More actions

    OK I made a few tiny tweaks

    diff --git a/gcloud/bigtable/client.py b/gcloud/bigtable/client.py
    index 35ca65c..aa79733 100644
    --- a/gcloud/bigtable/client.py
    +++ b/gcloud/bigtable/client.py
    @@ -284,6 +284,7 @@ class Client(_ClientFactoryMixin, _ClientProjectMixin):
             :rtype: :class:`grpc.beta._stub._AutoIntermediary`
             :returns: A gRPC stub object.
             """
    +        print('Making data stub')
             return _make_stub(self, DATA_STUB_FACTORY_V2,
                               DATA_API_HOST_V2, DATA_API_PORT_V2)
    
    @@ -293,6 +294,7 @@ class Client(_ClientFactoryMixin, _ClientProjectMixin):
             :rtype: :class:`grpc.beta._stub._AutoIntermediary`
             :returns: A gRPC stub object.
             """
    +        print('Making instance stub')
             return _make_stub(self, INSTANCE_STUB_FACTORY_V2,
                               INSTANCE_ADMIN_HOST_V2, INSTANCE_ADMIN_PORT_V2)
    
    @@ -305,6 +307,7 @@ class Client(_ClientFactoryMixin, _ClientProjectMixin):
             :rtype: :class:`grpc.beta._stub._AutoIntermediary`
             :returns: A gRPC stub object.
             """
    +        print('Making operations stub')
             return _make_stub(self, OPERATIONS_STUB_FACTORY_V2,
                               OPERATIONS_API_HOST_V2, OPERATIONS_API_PORT_V2)
    
    @@ -314,6 +317,7 @@ class Client(_ClientFactoryMixin, _ClientProjectMixin):
             :rtype: :class:`grpc.beta._stub._AutoIntermediary`
             :returns: A gRPC stub object.
             """
    +        print('Making table stub')
             return _make_stub(self, TABLE_STUB_FACTORY_V2,
                               TABLE_ADMIN_HOST_V2, TABLE_ADMIN_PORT_V2)
    
    @@ -432,13 +436,21 @@ class _MetadataPlugin(object):
                        Provides authorization and user agent.
         """
    
    +    COUNT = 0
    +
         def __init__(self, client):
    +        _MetadataPlugin.COUNT += 1
    +        self.curr_id = _MetadataPlugin.COUNT
    +        print('MetadataPlugin %d created' % (self.curr_id,))
             self._credentials = client.credentials
             self._user_agent = client.user_agent
    
         def __call__(self, unused_context, callback):
             """Adds authorization header to request metadata."""
    -        access_token = self._credentials.get_access_token().access_token
    +        access_token = self._credentials.get_access_token()
    +        print('plugin %s, expires in %r' % (self.curr_id,
    +                                            access_token.expires_in))
    +        access_token = access_token.access_token
             headers = [
                 ('Authorization', 'Bearer ' + access_token),
                 ('User-agent', self._user_agent),

    and effectively verified there is no caching.

  7. dhermes commented on Jul 18, 2016

    @dhermes
    dhermes
    on Jul 18, 2016
    Contributor
    More actions

    @sduskis I don't think there is enough in the SO question to replicate, I asked the author to weigh in here. @jonparrott Can you confirm that Python Flexible on GAE will use GCE tokens when doing auth?

  8. herbertrlee commented on Jul 18, 2016

    @herbertrlee
    herbertrlee
    on Jul 18, 2016
    More actions

    @dhermes, I am using the gae flexible environment. If there are any steps you'd like me to take to collect more information, let me know.

  9. theacodes commented on Jul 18, 2016

    @theacodes
    theacodes
    on Jul 18, 2016
    Contributor
    More actions

    The Python compat runtime can end up using App Engine identity credentials,
    iirc. The non-compat runtime uses GCE every time.

    On Mon, Jul 18, 2016, 3:23 PM herbertrlee [email protected] wrote:

    @dhermes https://github.com/dhermes, I am using the gae flexible
    environment. If there are any steps you'd like me to take to collect more
    information, let me know.

    —
    You are receiving this because you were mentioned.

    Reply to this email directly, view it on GitHub
    #1917 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AAPUcxVkAQKRdBDAwU76OTL8rTN8M6irks5qW_z-gaJpZM4I_SC3
    .

  10. dhermes commented on Jul 18, 2016

    @dhermes
    dhermes
    on Jul 18, 2016
    Contributor
    More actions

    @herbertrlee Can you consistently reproduce this? Can I send you some modifications so you can log the same bits I did above so we can tell where the bad token issue is occurring?

  11. herbertrlee commented on Jul 27, 2016

    @herbertrlee
    herbertrlee
    on Jul 27, 2016
    More actions

    @dhermes Sorry, I have been traveling the last week or so. Certainly, send them to me and I'll attempt to collect some logs.

  12. dhermes commented on Jul 27, 2016

    @dhermes
    dhermes
    on Jul 27, 2016
    Contributor
    More actions

    First, start with a fresh virtualenv so we can edit the files without feeling bad about it

    $ virtualenv repro-1917
    $ source repro-1917/bin/activate
    (repro-1917) $ pip install --upgrade gcloud
    

    then edit repro-1917/lib/python2.7/site-packages/gcloud/bigtable/client.py with my diff above.

    You'll also want to wrap your code that fails with a trace so you can step in and find out the state of things

    try:
        main()
    except:
        import pdb
        pdb.set_trace()
  13. herbertrlee commented on Aug 8, 2016

    @herbertrlee
    herbertrlee
    on Aug 8, 2016
    More actions

    @dhermes - Using gcloud 0.18.0 (previously 0.17.0), oauthclient 3.0.0 (previously 2.2.0), and grpcio 1.0.0rc1 (previously 0.15.0), I no longer seem to be able to reproduce this issue. I do still occasionally get auth errors, but I recover from them on subsequent attempts without needing to redeploy my app, which I can handle on my side. Thanks for your help and patience!

    Reacted by Misha Brukman
  14. dhermes commented on Aug 8, 2016

    @dhermes
    dhermes
    on Aug 8, 2016
    Contributor
    More actions

    w00t! Thanks for the reply. Feel free to ping this issue if the problem comes back.

  15. dhermes
    closed this as completedon Aug 8, 2016
  16. dr3x commented on May 4, 2017

    @dr3x
    dr3x
    on May 4, 2017
    More actions

    I'm getting a similar error, appears to be an auth timeout; basically setting up a Spanner client and pushing data to a table using a batch insert; finding that somewhere between 1-2mm records I get this auth error. Let me know if you want me to write this up with a separate issue.

    Traceback (most recent call last):
      File "C:\src\GitHub\python-docs-samples\quick_table_load.py", line 77, in <module>
        load_table(t)
      File "C:\src\GitHub\python-docs-samples\quick_table_load.py", line 66, in load_table
        insert_batch(table_name,cols,row_count,tsv_arr)
      File "C:\src\GitHub\python-docs-samples\quick_table_load.py", line 36, in insert_batch
        values=tsv_arr)
      File "C:\src\GitHub\python-docs-samples\env\lib\site-packages\google\cloud\spanner\database.py", line 462, in __exit__
        self._batch.commit()
      File "C:\src\GitHub\python-docs-samples\env\lib\site-packages\google\cloud\spanner\batch.py", line 156, in commit
        options=options)
      File "C:\src\GitHub\python-docs-samples\env\lib\site-packages\google\cloud\gapic\spanner\v1\spanner_client.py", line 832, in commit
        return self._commit(request, options)
      File "C:\src\GitHub\python-docs-samples\env\lib\site-packages\google\gax\api_callable.py", line 419, in inner
        return api_caller(api_call, this_settings, request)
      File "C:\src\GitHub\python-docs-samples\env\lib\site-packages\google\gax\api_callable.py", line 407, in base_caller
        return api_call(*args)
      File "C:\src\GitHub\python-docs-samples\env\lib\site-packages\google\gax\api_callable.py", line 368, in inner
        return a_func(*args, **kwargs)
      File "C:\src\GitHub\python-docs-samples\env\lib\site-packages\google\gax\retry.py", line 126, in inner
        ' classified as transient', exception)
    RetryError: GaxError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.UNAUTHENTICATED, Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.)>)
  17. dhermes commented on May 4, 2017

    @dhermes
    dhermes
    on May 4, 2017
    Contributor
    More actions

    @dr3x more crucial than 1-2mm records it the length of time that has elapsed. In general, the token's will only be good for an hour, but google-auth should handle the refresh just fine (i.e. this error is still strange to see, so worth digging into for us).

  18. dr3x commented on May 5, 2017

    @dr3x
    dr3x
    on May 5, 2017
    More actions

    ok will try an explicit auth refresh every 45 minutes and see if that resolves it - thanks

  19. dr3x commented on May 5, 2017

    @dr3x
    dr3x
    on May 5, 2017
    More actions

    Got Go the issue again with explicit refresh.

    This is the code I'm using; invoking the refresh method when there is less than 15 minutes to expiration:

    credentials, project_id = google.auth.default()
    print("Starting data load in project {}".format(project_id))
    request = google.auth.transport.requests.Request()
    credentials.refresh(request)

    I'm using the credentials.expiry property to know when to refresh.

    Running it again now but my code looks like it is doing what it is supposed to - expiry back at an hour after refresh.

  20. dhermes commented on May 5, 2017

    @dhermes
    dhermes
    on May 5, 2017
    Contributor
    More actions

    @dr3x Sorry I'm having trouble parsing what you've said. Are you said that when you explicitly call

    credentials.refresh(request)

    it fails (even after 45 minutes)?

  21. dr3x commented on May 5, 2017

    @dr3x
    dr3x
    on May 5, 2017
    More actions

    Still getting the unauthorized error - not sure if it is the refresh invocation or some other operation that is throwing the error:

      File "C:\src\github\python-docs-samples\env\lib\site-packages\google\gax\retry.py", line 127, in inner
        ' classified as transient', exception)
    google.gax.errors.RetryError: GaxError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.UNAUTHENTICATED, Request
    had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
    )>)

    Rerunning now and outputting the expiry time remaining every time I do a batch insert.

  22. dr3x commented on May 5, 2017

    @dr3x
    dr3x
    on May 5, 2017
    More actions

    Strange - based on the log - looks like it failed at an hour, even though the expiry showed it had more time?

    Timeline:
    Start 00:00
    Refresh 00:45
    Auth Error 00:60 (which is 15 minutes after successfully invoking refresh)

    ...
    Inserting batch: 1337671, Table: Some_Table, Expiration: 0:45:01.936000
    Inserting batch: 1338172, Table: Some_Table, Expiration: 0:45:00.733000

    Traceback (most recent call last):
      File "quick_table_load.py", line 124, in <module>
        load_table(table_name)
      File "quick_table_load.py", line 110, in load_table
        insert_batch(table_name,col_names,chgs,tsv_arr)
      File "quick_table_load.py", line 80, in insert_batch
        values=tsv_arr)
      File "C:\src\github\python-docs-samples\env\lib\site-packages\google\cloud\spanner\database.py", line 462, in __exit__
        self._batch.commit()
      File "C:\src\github\python-docs-samples\env\lib\site-packages\google\cloud\spanner\batch.py", line 156, in commit
        options=options)
      File "C:\src\github\python-docs-samples\env\lib\site-packages\google\cloud\gapic\spanner\v1\spanner_client.py", line 832, in commit
        return self._commit(request, options)
      File "C:\src\github\python-docs-samples\env\lib\site-packages\google\gax\api_callable.py", line 428, in inner
        return api_caller(api_call, this_settings, request)
      File "C:\src\github\python-docs-samples\env\lib\site-packages\google\gax\api_callable.py", line 416, in base_caller
        return api_call(*args)
      File "C:\src\github\python-docs-samples\env\lib\site-packages\google\gax\api_callable.py", line 376, in inner
        return a_func(*args, **kwargs)
      File "C:\src\github\python-docs-samples\env\lib\site-packages\google\gax\retry.py", line 127, in inner
        ' classified as transient', exception)
    google.gax.errors.RetryError: GaxError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.UNAUTHENTICATED, Request
    had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
    )>)
    
    (env) C:\src\github\python-docs-samples>
  23. theacodes
    unassigned
    dhermes
    on Sep 28, 2018
  24. JustinBeckwith
    assigned
    dhermes
    on Feb 1, 2021
  25. parthea
    added a commit that references this issue on Oct 21, 2023

    chore: move samples from python-docs-sample (#45)

    d369a34
  26. parthea
    added a commit that references this issue on Mar 6, 2026

    feat: adds timer decorator to facilitate debugging (#1917)

    9002358
  27. parthea
    added a commit that references this issue on Apr 1, 2026

    fix: correct min field from max() to min() in remote function tests (#…

    e848fed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @dhermes
    dhermes

Labels

api: bigtableIssues related to the Bigtable API.auth

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.