Auf dieser Seite werden die grundlegenden Schritte beschrieben, die erforderlich sind, um die Service Control API auf Ihrem lokalen Computer einzurichten und mit dem Befehl curl
zu testen.
Ersteinrichtung
Die Service Control API arbeitet mit verwalteten Diensten zusammen. Um die Service Control API zu verwenden, müssen Sie zuerst einen verwalteten Dienst mithilfe der Service Management API erstellen. Weitere Informationen finden Sie unter Service Management – Erste Schritte.
Nachdem Sie einen verwalteten Dienst erstellt haben, müssen Sie folgende Schritte ausführen, bevor Sie die Service Control API über Ihren verwalteten Dienst verwenden.
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden.
-
Führen Sie folgenden Befehl aus, um die gcloud CLI zu initialisieren:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Service Control API:
gcloud services enable servicecontrol.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/servicemanagement.serviceController
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: your project ID.USER_IDENTIFIER
: the identifier for your user account—for example,[email protected]
.ROLE
: the IAM role that you grant to your user account.
-
Install the Google Cloud CLI.
-
Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden.
-
Führen Sie folgenden Befehl aus, um die gcloud CLI zu initialisieren:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Service Control API:
gcloud services enable servicecontrol.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/servicemanagement.serviceController
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: your project ID.USER_IDENTIFIER
: the identifier for your user account—for example,[email protected]
.ROLE
: the IAM role that you grant to your user account.
- Sie haben eine funktionsfähige lokale Testumgebung, in der Sie beliebige Google Cloud Platform APIs aufrufen können.
- Sie haben einen funktionsfähigen Dienst, den Sie mit der Service Management API und der Service Control API verwenden können.
- Sie haben ein Dienstkonto mit den entsprechenden Berechtigungen, das Sie für die Ausführung Ihres Dienstes verwenden können.
Mit curl testen
Definieren Sie zuerst ein geeignetes Shell-Alias zum Aufrufen der Google REST APIs:
alias gcurl='curl -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" '
Die folgende Abfolge von Shell-Befehlen zeigt die Schritte für den Aufruf der Service Control API.
# Call with invalid service name "invalid.com". For security and privacy # reasons, the permission check typically happens before other checks. $ gcurl -d '{}' https://servicecontrol.googleapis.com/v1/services/invalid.com:check { "error": { "code": 403, "message": "Permission 'servicemanagement.services.check' denied on service 'invalid.com'.", "status": "PERMISSION_DENIED" } } # Call without proper permission on a service. $ gcurl -d '{}' https://servicecontrol.googleapis.com/v1/services/servicecontrol.googleapis.com:check { "error": { "code": 403, "message": "Permission 'servicemanagement.services.check' denied on service 'servicecontrol.googleapis.com'.", "status": "PERMISSION_DENIED" } } # Call with invalid request. $ gcurl -d '{}' https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:check { "error": { "code": 400, "message": "Request contains an invalid argument.", "status": "INVALID_ARGUMENT" } } # This and following call assume that the service, operation name and # project being checked are "endpointsapis.appspot.com", # "google.example.hello.v1.HelloService.GetHello" and # "endpointsapis-consumer" correspondingly. # Change to the name of your managed service, operation, and project. # Call with invalid request. $ gcurl -d '{ "operation": { "operationId": "123e4567-e89b-12d3-a456-426655440000", "consumerId": "project:endpointsapis-consumer", "startTime": "2016-06-12T22:00:15Z", "operationName": "google.example.hello.v1.HelloService.GetHello" } }' https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:check { "checkErrors": [ { "code": "SERVICE_NOT_ACTIVATED", "detail": "Service 'endpointsapis.appspot.com' is not enabled for consumer 'project:endpointsapis-consumer'." } ] } # Successful call to "services.check" method after the API is enabled for # the project. $ gcurl -d '{ "operation": { "operationId": "123e4567-e89b-12d3-a456-426655440000", "consumerId": "project:endpointsapis-consumer", "startTime":"2016-07-31T05:20:00Z", "operationName":"google.example.hello.v1.HelloService.GetHello" } }' https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:check { "operationId": "123e4567-e89b-12d3-a456-426655440000" }
Ergebnis nach Ausführung der vorherigen Schritte: