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 — set correct project when using from_service_account_json · Issue #1883 · googleapis/google-cloud-python

set correct project when using from_service_account_json #1883

New issue
Copy link
New issue
Copy link
Closed
Closed
set correct project when using from_service_account_json#1883
Copy link
Assignees
lukesneeringer
Labels
api: coreauthpriority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@kelvinabrokwa
kelvinabrokwa
opened on Jun 21, 2016
Issue body actions

When instantiating with bigquery.Client.from_service_account_json('...') the project field in the client should be set to the project_id defined in the service account JSON.

Right now it is still the one from env.

Activity

Next
  1. daspecster
    added
    auth
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Jun 21, 2016
  2. daspecster commented on Jun 21, 2016

    @daspecster
    daspecster
    on Jun 21, 2016
    Contributor
    More actions

    Thanks for reporting @kelvinabrokwa!

    I was able to reproduce the issue with

    $ export GCLOUD_PROJECT=testing-project
    >>> from gcloud import bigquery
    >>> import os
    >>> os.getenv('GCLOUD_PROJECT')
    'testing-project'
    >>> bc = bigquery.Client.from_service_account_json('creds.json')
    >>> bc.project
    u'testing-project'

    I'll try and figure out what's up.

  3. daspecster
    self-assigned this
    on Jun 21, 2016
  4. kelvinabrokwa commented on Jun 21, 2016

    @kelvinabrokwa
    kelvinabrokwa
    on Jun 21, 2016
    Author
    More actions

    @daspecster thanks for the quick response!

  5. dhermes commented on Jun 21, 2016

    @dhermes
    dhermes
    on Jun 21, 2016
    Contributor
    More actions

    The credentials check and project check are completely decoupled. The from_service_account_* helpers pass an explicit credentials instance, hence they bypass the check. We may want to also pass an explicit project.

  6. daspecster commented on Jun 22, 2016

    @daspecster
    daspecster
    on Jun 22, 2016
    Contributor
    More actions

    @dhermes, what do you think about just adding/calling a helper function to extract the project id from the file at this point?

    https://github.com/GoogleCloudPlatform/gcloud-python/blob/master/gcloud/client.py#L60

  7. daspecster
    added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    and removed
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Jun 22, 2016
  8. jgeewax commented on Jun 22, 2016

    @jgeewax
    jgeewax
    on Jun 22, 2016
    Contributor
    More actions

    This is actually tricky because a service account can have access to multiple different projects (ie, you can go into another project and add that service account to the project so it has access over there).

    If we were to automagically pick up the project ID from the key file (JSON), how would people specify "I'm using a service account from project A to talk to resources in project B" ?

  9. daspecster commented on Jun 22, 2016

    @daspecster
    daspecster
    on Jun 22, 2016
    Contributor
    More actions

    True @jgeewax, similar issue in #1827 right?

  10. jgeewax commented on Jun 22, 2016

    @jgeewax
    jgeewax
    on Jun 22, 2016
    Contributor
    More actions

    Yea I'd say these are related.

  11. kelvinabrokwa commented on Jun 22, 2016

    @kelvinabrokwa
    kelvinabrokwa
    on Jun 22, 2016
    Author
    More actions

    @jgeewax good point. I actually just ran into that today.

    The way I'm looking at it now, the project_id in the JSON key file is only an indicator of where the service account lives, not necessarily which project it can talk to.

    With that in mind, I can definitely imagine a scenario in which someone sets their env var purposefully to the project they want and then run into a bug where the library is using the one from the key file.

    Its more a question of developer experience at this point.

  12. jgeewax commented on Jun 23, 2016

    @jgeewax
    jgeewax
    on Jun 23, 2016
    Contributor
    More actions

    I could be wrong, but this might be one of those special situations where we should try to separate a client from credentials, as well as loading a client from a service account from loading credentials from a service account.

    For example, when I load a Client from a service account, I'm expecting that all data in the service account is used to create a client. I think you're right in saying this should pull a project ID if that's there.

    On the other hand, if I were to create a client and set the credentials to ones from a service account, I'm expecting that it only reads in the credential, and pulls the project based on the default patterns.

    from gcloud import bigquery
    
    # Get all defaults: credentials, project ID, scope, etc.
    client = bigquery.Client()
    
    # Get everything we possibly can from the service account JSON file
    client = bigquery.Client.from_service_account_json('key.json')
    
    # Get credentials from the service account JSON file, and defaults for everything else
    client = bigquery.Client(credentials=Credentials.from_service_account_json('key.json'))
    
    # Use key.json for credentials, and 'my-project' as the project ID
    client = bigquery.Client(project='my-project',
                             credentials=Credentials.from_service_account_json('key.json'))

    @tseaver , @daspecster : Is this something that would make sense across the project?

    Reacted by Jiwon Kang
  13. daspecster commented on Jun 23, 2016

    @daspecster
    daspecster
    on Jun 23, 2016
    Contributor
    More actions

    That makes sense to me at first glance.

    In the last example, that would override envar GCLOUD_PROJECT, correct?

    For the second example,

    # Get everything we possibly can from the service account JSON file
    client = bigquery.Client.from_service_account_json('key.json')

    If the project ID isn't in the key.json then it would run through the default search order?
    There's a chance that a user might expect the project ID to be in key.json but it's not and then we would want to be pretty clear on what's happening.

    In an application where multiple projects are used, I would assume that I would have to specify each one as I wanted to access it.

  14. 10 remaining items

  15. dhermes
    reopened this on Apr 19, 2017
  16. dhermes commented on Apr 19, 2017

    @dhermes
    dhermes
    on Apr 19, 2017
    Contributor
    More actions

    @lukesneeringer This is still a bug

  17. dhermes
    added a commit that references this issue on May 17, 2017

    Adding optional switch to capture project ID in from_service_account_…

    This issue will close once commit d67cf45 is merged into the 'master' branch.
    d67cf45
  18. dhermes
    closed this as completedin #3436on Jun 6, 2017
  19. dhermes
    added a commit that references this issue on Jun 6, 2017

    Adding optional switch to capture project ID in from_service_account_…

    This issue will close once commit a55010f is merged into the 'main' branch.
    a55010f
  20. landrito
    added 3 commits that reference this issue on Aug 21, 2017

    Adding optional switch to capture project ID in from_service_account_…

    This issue will close once commit d833cd0 is merged into the 'master' branch.
    d833cd0

    Adding optional switch to capture project ID in from_service_account_…

    This issue will close once commit 9b7249c is merged into the 'master' branch.
    9b7249c

    Adding optional switch to capture project ID in from_service_account_…

    This issue will close once commit 4f938d6 is merged into the 'master' branch.
    4f938d6
  21. JustinBeckwith
    assigned
    lukesneeringer
    on Feb 1, 2021
  22. parthea
    added 2 commits that reference this issue on Nov 22, 2025

    Adding optional switch to capture project ID in from_service_account_…

    This issue will close once commit 75f2d0c is merged into the 'main' branch.
    75f2d0c

    Adding optional switch to capture project ID in from_service_account_…

    This issue will close once commit c7be5a7 is merged into the 'main' branch.
    c7be5a7
  23. parthea
    added 4 commits that reference this issue on Mar 2, 2026

    chore(tests): allow expired secret in system tests (#1883)

    27323e9

    test: handle invalid_grant in default creds system tests (#1901)

    7f96abb

    chore(main): release 3.21.0 (#1883)

    da365f6

    Adding optional switch to capture project ID in from_service_account_…

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

Metadata

Metadata

Assignees

  • @lukesneeringer
    lukesneeringer

Labels

api: coreauthpriority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

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.