1

Wanted to call out one GCP api from my python code, following is that method -

import google
from google.oauth2 import service_account
import google.auth.transport.requests
import requests

def get_window(self):
    credentials = service_account.Credentials.from_service_account_file(
        self.default_credentials, scopes=['https://www.googleapis.com/auth/cloud-platform']
    )
    request = google.auth.transport.requests.Request()
    credentials.refresh(request)

    # Make an authenticated API request
    request_url = "https://sqladmin.googleapis.com/v1/projects/my-projectid/instances/my-instanceid"
    headers = {"Authorization", f"Bearer {credentials.token}"}
    req = requests.get(request_url, headers=headers)
    print(req.text)

When I call this method, following is the response:

request = google.auth.transport.requests.Request()
AttributeError: module 'google.auth.transport' has no attribute 'requests'

I am using python 3.9 on my local install packages are:

pip freeze

aiohttp==3.8.1
aiosignal==1.2.0                 
async-timeout==4.0.2             
attrs==21.4.0                    
cachetools==5.0.0                
certifi==2021.10.8               
googleapis-common-protos==1.56.0
httplib2==0.20.4
idna==3.3
multidict==6.0.2
protobuf==3.19.4
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.21
pyOpenSSL==22.0.0
pyparsing==3.0.7
requests==2.27.1
rsa==4.8
six==1.16.0
uritemplate==4.1.1
urllib3==1.26.9
yarl==1.7.2
5
  • 2
    Remove import google. That is a deprecated package that is breaking your other code. Commented Mar 21, 2022 at 19:23
  • Your import list shows that you have not installed the required Google Python packages. Follow the documentation to set up the Python SDK. cloud.google.com/python/docs/setup Commented Mar 21, 2022 at 19:28
  • Thanks @JohnHanley I am able to run my code now Commented Mar 21, 2022 at 19:31
  • What was the solution? Commented Mar 21, 2022 at 19:31
  • I installed the google auth library and used import for right module which I missed from the last thread. Commented Mar 21, 2022 at 19:45

2 Answers 2

1

google.auth.transport is a package, not a module, so the correct way to import it would be

import google.auth.transport.requests

or if you prefer

from google.auth.transport import requests
Sign up to request clarification or add additional context in comments.

Comments

0

For me it worked as follows:
I am using python version 3.9

import google.auth
import google.oauth2.id_token
creds, _ = google.auth.default()
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
response = requests.post(
        f"{url}/route_here",
        json=payload,
        headers={
            "Content-Type": "application/json",
            "Authorization": f"Bearer {creds.id_token}",
        },
    )

To use Bearer use id_token inside creds, to use OIDC use only token

Docs: https://cloud.google.com/functions/docs/samples/functions-bearer-token?hl=pt-br#functions_bearer_token-python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.