question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'Credentials' object has no attribute 'with_scopes'

See original GitHub issue

Reproduce:

python3 -m venv cloudsql_env
source cloudsql_env/bin/activate
pip install git+https://github.com/GoogleCloudPlatform/cloud-sql-python-connector@5b02e80149864369a0b8f6bc142d55c278a6e796

ipython
> from google.cloud.sql.connector import connector
> conn = connector.connect("<my-project>:us-central1:sql-1", "pymysql", user="root", password="<my-password", db="<my-db>")

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-2f8a588de201> in <module>
      4     user="root",
      5     password="<>",
----> 6     db="<>")

~/workspace/scio/cloudsql_env/lib/python3.7/site-packages/google/cloud/sql/connector/connector.py in connect(instance_connection_string, driver, **kwargs)
     66 
     67     loop = _get_loop()
---> 68     icm = InstanceConnectionManager(instance_connection_string, loop)
     69     return icm.connect(driver, user=kwargs.pop("user"), **kwargs)

~/workspace/scio/cloudsql_env/lib/python3.7/site-packages/google/cloud/sql/connector/InstanceConnectionManager.py in __init__(self, instance_connection_string, loop)
    152 
    153         self._loop = loop
--> 154         self._auth_init()
    155         self._priv_key, pub_key = generate_keys()
    156         self._pub_key = pub_key.decode("UTF-8")

~/workspace/scio/cloudsql_env/lib/python3.7/site-packages/google/cloud/sql/connector/InstanceConnectionManager.py in _auth_init(self)
    373 
    374         credentials, project = google.auth.default()
--> 375         scoped_credentials = credentials.with_scopes(
    376             [
    377                 "https://www.googleapis.com/auth/sqlservice.admin",

AttributeError: 'Credentials' object has no attribute 'with_scopes'

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
crwilcoxcommented, Mar 31, 2020

I dug into the auth thing. I am going to pass the scope to default. Really, it should avoid a duplication of auth so just a bit cleaner. PR incoming.

1reaction
eddie-sciocommented, Mar 31, 2020

Thanks for the quick reply! IIUC, the Credentials object that is returned may not have a with_scopes implementation (see the base class), as it may not be a child of Scoped. I believe these are the type of Credentials I get when I run with my owner account, as opposed to with a service account. In my case, the credentials creation goes down this path, which is a base Credentials object. This is also why if you have a service account activated, or if you’re running on App Engine / GCE this logic is fine.

You’re right that the scopes themselves are required – I think with_scopes_if_required is a bit of a misleading name.

Perhaps a more clear thing to do would be to change

        credentials, project = google.auth.default()
        scoped_credentials = credentials.with_scopes(
            [
                "https://www.googleapis.com/auth/sqlservice.admin",
                "https://www.googleapis.com/auth/cloud-platform",
            ]
        )

to

 credentials, project = google.auth.default(scopes=[
                "https://www.googleapis.com/auth/sqlservice.admin",
                "https://www.googleapis.com/auth/cloud-platform",
            ]
)

here. Note that will result in the same logic in the underlying auth library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GAE AttributeError: 'Credentials' object has no attribute ...
Problem is on GAE, when deployed, the call to with_subject raises: AttributeError: 'Credentials' object has no attribute 'with_subject'.
Read more >
AttributeError: 'Credentials' object has no attribute 'authorize'
I am getting following error while deploying the app. I have changed peoject Id in config file. ERROR: (gcloud.app.deploy) Error Response: [9]
Read more >
[Python] Gmail API AttributeError: 'Credentials' object has no ...
I'm fairly certain my credentials and permissions are correct, and it's an issue with the code, but I'm really at a loss as...
Read more >
google-auth Documentation - Read the Docs
Credentials are the means of identifying an application or user to a service or API. Credentials can be obtained with three different types ......
Read more >
OAuth2 scopes - FastAPI
The content of each of these strings can have any format, but should not ... credentials_exception for scope in security_scopes.scopes: if scope not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found