googleapis / google-api-python-client

🐍 The official Python client library for Google's discovery based APIs.
https://googleapis.github.io/google-api-python-client/docs/
Apache License 2.0
7.69k stars 2.4k forks source link

UnknownApiNameOrVersion exception with commentanalyzer #1238

Closed enkeyz closed 3 years ago

enkeyz commented 3 years ago

Environment details

Steps to reproduce

Just got my API access from them, made a project, added an API key, and their sample code wouldn't work.

  1. Run code - it's from the offical website https://support.perspectiveapi.com/s/docs-sample-requests

Code example

from googleapiclient import discovery
import json

API_KEY = ""

# Generates API client object dynamically based on service name and version.
service = discovery.build("commentanalyzer", "v1alpha1", developerKey=API_KEY)

analyze_request = {
    "comment": {"text": "friendly greetings from python"},
    "requestedAttributes": {"TOXICITY": {}},
}

response = service.comments().analyze(body=analyze_request).execute()

print(json.dumps(response, indent=2))

Stack trace

Traceback (most recent call last):
  File "/home/enkeyz/stuff/discord/detox.ai/bot/test.py", line 7, in <module>
    service = discovery.build("commentanalyzer", "v1alpha1", developerKey=API_KEY)
  File "/home/enkeyz/.local/share/virtualenvs/detox.ai-WfgfN2Jd/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/enkeyz/.local/share/virtualenvs/detox.ai-WfgfN2Jd/lib/python3.9/site-packages/googleapiclient/discovery.py", line 273, in build
    content = _retrieve_discovery_doc(
  File "/home/enkeyz/.local/share/virtualenvs/detox.ai-WfgfN2Jd/lib/python3.9/site-packages/googleapiclient/discovery.py", line 387, in _retrieve_discovery_doc
    raise UnknownApiNameOrVersion("name: %s  version: %s" % (serviceName, version))
googleapiclient.errors.UnknownApiNameOrVersion: name: commentanalyzer  version: v1alpha1
jdelalande commented 3 years ago

Hello,

I am having the same issue on mybusiness v4 since march 4th. Connection was allrigth until then (using this connection for several months).

` from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request

print(creds.valid) import sys print(sys.version)

print (googleapiclient.version)

SCOPES = ['https://www.googleapis.com/auth/business.manage'] discovery_uri = "https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json" service = build('mybusiness', 'v4', credentials=creds, discoveryServiceUrl = discovery_uri)`

`True 3.7.5 (default, Nov 7 2019, 10:50:52) [GCC 8.3.0] UnknownApiNameOrVersion Traceback (most recent call last)

in 5 SCOPES = ['https://www.googleapis.com/auth/business.manage'] 6 discovery_uri = "https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json" ----> 7 service = build('mybusiness', 'v4', credentials=creds, discoveryServiceUrl = discovery_uri) /databricks/python/lib/python3.7/site-packages/googleapiclient/_helpers.py in positional_wrapper(*args, **kwargs) 132 elif positional_parameters_enforcement == POSITIONAL_WARNING: 133 logger.warning(message) --> 134 return wrapped(*args, **kwargs) 135 136 return positional_wrapper /databricks/python/lib/python3.7/site-packages/googleapiclient/discovery.py in build(serviceName, version, http, discoveryServiceUrl, developerKey, model, requestBuilder, credentials, cache_discovery, cache, client_options, adc_cert_path, adc_key_path, num_retries, static_discovery) 280 developerKey, 281 num_retries=num_retries, --> 282 static_discovery=static_discovery, 283 ) 284 service = build_from_document( /databricks/python/lib/python3.7/site-packages/googleapiclient/discovery.py in _retrieve_discovery_doc(url, http, cache_discovery, serviceName, version, cache, developerKey, num_retries, static_discovery) 385 return content 386 else: --> 387 raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, version)) 388 389 actual_url = url UnknownApiNameOrVersion: name: mybusiness version: v4`
oliverkostoff commented 3 years ago

I had something very similar on Python 3.8.8 ( from this image https://hub.docker.com/_/python, tag: 3.8 ) from GCP CloudRun container:

Traceback (most recent call last): File "/usr/local/bin/bottle.py", line 868, in _handle return route.call(**args) File "/usr/local/bin/bottle.py", line 1748, in wrapper rv = callback(*a, **ka) File "/srv/main.py", line 187, in inventory gcp_client = GCPClient(project=PROJECT, region=REGION) File "/srv/main.py", line 46, in __init__ self.compute = googleapiclient.discovery.build('compute', 'v1') File "/usr/local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper return wrapped(*args, **kwargs) File "/usr/local/lib/python3.8/site-packages/googleapiclient/discovery.py", line 273, in build content = _retrieve_discovery_doc( File "/usr/local/lib/python3.8/site-packages/googleapiclient/discovery.py", line 387, in _retrieve_discovery_doc raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, version)) googleapiclient.errors.UnknownApiNameOrVersion: name: compute version: v1

This worked flawlessly till Thu last week. I now tested with newer image ( Python:3.9.2 ) and the error is gone. Please document any interpreter requirements, if any.

enkeyz commented 3 years ago

My problem is that my app needs this API to work. Development standing still until this issue resolved. Pretty sure this issue isn't completly related to this package, but Google's API platform.

busunkim96 commented 3 years ago

Hi folks,

Please see https://github.com/googleapis/google-api-python-client/issues/1225.

enkeyz commented 3 years ago

Sorry, but that doesn't solve any issue I have. How can I get the discovery url? That issue you linked doesn't really help at all.

busunkim96 commented 3 years ago

Hi @enkeyz,

From looking at the NodeJS sample linked on the documentation page, it looks like 'https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1' is the discovery URL.

from googleapiclient import discovery

client = discovery.build(
    "commentanalyzer",
    "v1alpha1",
    discoveryServiceUrl="https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1",
    static_discovery=False,
)

I'll reach out to the comment analyzer team to see if they can get their API listed in the directory.

busunkim96 commented 3 years ago

Filed 82170350 internally to ask the API owners to update the sample or list the API in the directory.