googleapiclient.errors UnknownApiNameOrVersion
Hi, I keep getting the "UnknownApiNameOrVersion " , dont know why. It worked once and returned the user profile expected but since then its been failing with this error for the entire day of Dec 12 2022 (PST). The code has not changed.
Environment details
- OS type and version: Windows 11 Pro
- Python version: Python 3.8
- pip version:
pip --version google-api-python-clientversion: 2.65.0
Code example
credentials = ServiceAccountCredentials.from_json_keyfile_dict(data, API_SCOPES)
# end here
# If no credentials were found, throw an error
if credentials is None or credentials.invalid:
#credentials = tools.run_flow(flow, storage,tools.argparser.parse_known_args()[0])
print('Invalid Credentials')
print("ERROR: Request Error: %s" % e)
sys.exit(1)
# Use the credentials to authorize an httplib2.Http instance.
http = credentials.authorize(httplib2.Http())
# Construct and return a service object via the discovery service.
#service = discovery.build(API_NAME, API_VERSION, http=http, discoveryServiceUrl='https://dfareporting.googleapis.com') ## this line of code is failing
#service = discovery.build(API_NAME, API_VERSION, http=http, discoveryServiceUrl='https://dfareporting.googleapis.com',static_discovery=False) ## this line of code is failing
# Create a client for the Campaign Manager API using the credentials object
**service = discovery.build('dfareporting', 'v4.0', credentials=credentials,static_discovery=False)**
# Make a request to the API using the client
response = service.userProfiles().list().execute()
print(response)
#### Stack trace
{
"errorMessage": "name: dfareporting version: v4.0",
"errorType": "UnknownApiNameOrVersion",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 71, in lambda_handler\n service = discovery.build('dfareporting', 'v4.0', credentials=credentials,static_discovery=False)\n",
" File \"/opt/python/googleapiclient/_helpers.py\", line 130, in positional_wrapper\n return wrapped(*args, **kwargs)\n",
" File \"/opt/python/googleapiclient/discovery.py\", line 324, in build\n raise UnknownApiNameOrVersion(\"name: %s version: %s\" % (serviceName, version))\n"
]
}
Making sure to follow these steps will guarantee the quickest resolution possible.
Thanks!
@harbd The error message "UnknownApiNameOrVersion" indicates that the API name or version specified in the discovery.build() method call is not valid. In this case, the API name is "dfareporting" and the version is "v4.0"!
Instead of :
discovery.build('dfareporting', 'v4.0', credentials=credentials,static_discovery=False)
try :
service = discovery.build('dfareporting', 'v4', credentials=credentials)
If it doesn't work, I suggest you double-check that the API name and version you are using are correct and match the values in the Google API documentation. Additionally, check if the service is enabled in your Google cloud project. You might want to check the region settings and make sure that the service is enabled in the region where your app is running.