我无法理解 Google 产品/服务中使用的“API 发现”概念。以下是一些使用上述发现服务访问 Google Cloud Vision 的 Python 代码:
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials
…
API_DISCOVERY_FILE = 'https://vision.googleapis.com/$discovery/rest?version=v1'
hlh = httplib2.Http()
credentials = GoogleCredentials.get_application_default().create_scoped(
['https://www.googleapis.com/auth/cloud-platform'])
credentials.authorize(hlh)
service = build(serviceName='vision', version='v1', http=hlh, discoveryServiceUrl=API_DISCOVERY_FILE)
service_request = service.images().annotate(body={ <more JSON code here> })
这是另一段 Python 代码,它也访问 Google Cloud Vision,但不使用 API 发现并且工作得很好:
import requests
…
ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'
response = requests.post(ENDPOINT_URL,
data=make_image_data(image_filenames),
params={'key': api_key},
headers={'Content-Type': 'application/json'})
我无法理解的是这个问题:您需要知道您将要调用的 API 的详细信息,以便您可以定制调用;这是显而易见的。那么,API 发现在调用时如何帮助您,在您准备好调用该 API 的代码之后?
PS:在发布这个问题之前,我确实查看了以下资源:
https://developers.google.com/discovery/v1/getting_started https://developers.google.com/discovery/v1/getting_started
https://developers.google.com/discovery/v1/using https://developers.google.com/discovery/v1/using
我确实看到了this https://stackoverflow.com/questions/33846841/what-is-discovery-based-in-rest-api回答了问题,但希望有更多的见解。