In Google Cloud gehostete Dienste mit Zugriff auf den Compute Metadata Server können mithilfe der mit dem Dienst verknüpften Dienstkontoidentität ein OAuth-Authentifizierungstoken generieren. Mit diesem Token kann der Dienst als zulässiger Aufrufer eines Cloud Run-Dienstes authentifiziert werden.
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.IdTokenCredentials;
import com.google.auth.oauth2.IdTokenProvider;
import java.io.IOException;
public class Authentication {
// makeGetRequest makes a GET request to the specified Cloud Run or
// Cloud Functions endpoint `serviceUrl` (must be a complete URL), by
// authenticating with an ID token retrieved from Application Default
// Credentials using the specified `audience`.
//
// Example `audience` value (Cloud Run): https://my-cloud-run-service.run.app/
public static HttpResponse makeGetRequest(String serviceUrl, String audience) throws IOException {
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
if (!(credentials instanceof IdTokenProvider)) {
throw new IllegalArgumentException("Credentials are not an instance of IdTokenProvider.");
}
IdTokenCredentials tokenCredential =
IdTokenCredentials.newBuilder()
.setIdTokenProvider((IdTokenProvider) credentials)
.setTargetAudience(audience)
.build();
GenericUrl genericUrl = new GenericUrl(serviceUrl);
HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(tokenCredential);
HttpTransport transport = new NetHttpTransport();
HttpRequest request = transport.createRequestFactory(adapter).buildGetRequest(genericUrl);
return request.execute();
}
}
import urllib
import google.auth.transport.requests
import google.oauth2.id_token
def make_authorized_get_request(endpoint, audience):
"""
make_authorized_get_request makes a GET request to the specified HTTP endpoint
by authenticating with the ID token obtained from the google-auth client library
using the specified audience value.
"""
# Cloud Run uses your service's hostname as the `audience` value
# audience = 'https://my-cloud-run-service.run.app/'
# For Cloud Run, `endpoint` is the URL (hostname + path) receiving the request
# endpoint = 'https://my-cloud-run-service.run.app/my/awesome/url'
req = urllib.request.Request(endpoint)
auth_req = google.auth.transport.requests.Request()
id_token = google.oauth2.id_token.fetch_id_token(auth_req, audience)
req.add_header("Authorization", f"Bearer {id_token}")
response = urllib.request.urlopen(req)
return response.read()
Nächste Schritte
Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.