Skip to content

Commit 669ab04

Browse files
docs(samples): modified comments in the samples and minor refactor (#990)
* docs(samples): modified comments in the samples and minor refactor * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * modified comments acc to review Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 68ab03c commit 669ab04

File tree

6 files changed

+33
-37
lines changed

6 files changed

+33
-37
lines changed

samples/snippets/src/main/java/AuthenticateImplicitWithAdc.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616

1717
// [START auth_cloud_implicit_adc]
1818

19-
import com.google.cloud.compute.v1.Instance;
20-
import com.google.cloud.compute.v1.InstancesClient;
19+
import com.google.api.gax.paging.Page;
20+
import com.google.cloud.storage.Bucket;
21+
import com.google.cloud.storage.Storage;
22+
import com.google.cloud.storage.StorageOptions;
2123
import java.io.IOException;
2224

2325
public class AuthenticateImplicitWithAdc {
2426

2527
public static void main(String[] args) throws IOException {
2628
// TODO(Developer):
2729
// 1. Before running this sample,
28-
// set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc
30+
// set up Application Default Credentials as described in
31+
// https://cloud.google.com/docs/authentication/external/set-up-adc
2932
// 2. Replace the project variable below.
30-
// 3. Make sure that the user account or service account that you are using
31-
// has the required permissions. For this sample, you must have "compute.instances.list".
33+
// 3. Make sure you have the necessary permission to list storage buckets
34+
// "storage.buckets.list"
3235
String projectId = "your-google-cloud-project-id";
3336
authenticateImplicitWithAdc(projectId);
3437
}
@@ -37,24 +40,20 @@ public static void main(String[] args) throws IOException {
3740
// credentials to use.
3841
public static void authenticateImplicitWithAdc(String project) throws IOException {
3942

40-
String zone = "us-central1-a";
41-
// This snippet demonstrates how to list instances.
4243
// *NOTE*: Replace the client created below with the client required for your application.
4344
// Note that the credentials are not specified when constructing the client.
4445
// Hence, the client library will look for credentials using ADC.
4546
//
4647
// Initialize client that will be used to send requests. This client only needs to be created
47-
// once, and can be reused for multiple requests. After completing all of your requests, call
48-
// the `instancesClient.close()` method on the client to safely
49-
// clean up any remaining background resources.
50-
try (InstancesClient instancesClient = InstancesClient.create()) {
51-
// Set the project and zone to retrieve instances present in the zone.
52-
System.out.printf("Listing instances from %s in %s:", project, zone);
53-
for (Instance zoneInstance : instancesClient.list(project, zone).iterateAll()) {
54-
System.out.println(zoneInstance.getName());
55-
}
56-
System.out.println("####### Listing instances complete #######");
48+
// once, and can be reused for multiple requests.
49+
Storage storage = StorageOptions.newBuilder().setProjectId(project).build().getService();
50+
51+
System.out.println("Buckets:");
52+
Page<Bucket> buckets = storage.list();
53+
for (Bucket bucket : buckets.iterateAll()) {
54+
System.out.println(bucket.toString());
5755
}
56+
System.out.println("Listed all storage buckets.");
5857
}
5958
}
6059
// [END auth_cloud_implicit_adc]

samples/snippets/src/main/java/IdTokenFromImpersonatedCredentials.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ public static void main(String[] args) throws IOException {
3131

3232
// Provide the scopes that you might need to request to access Google APIs,
3333
// depending on the level of access you need.
34-
// The best practice is to use the cloud-wide scope and use IAM to narrow the permissions.
35-
// https://cloud.google.com/docs/authentication#authorization_for_services
34+
// This example uses the cloud-wide scope and uses IAM to narrow the permissions.
35+
// https://cloud.google.com/docs/authentication/external/authorization-gcp
3636
// For more information, see: https://developers.google.com/identity/protocols/oauth2/scopes
3737
String scope = "https://www.googleapis.com/auth/cloud-platform";
3838

39-
// The service name for which the id token is requested. Service name refers to the
40-
// logical identifier of an API service, such as "pubsub.googleapis.com".
41-
String targetAudience = "iap.googleapis.com";
39+
// The service name for which the id token is requested.
40+
String targetAudience = "https://example.com";
4241

4342
// The name of the privilege-bearing service account for whom the credential is created.
4443
String impersonatedServiceAccount = "name@project.service.gserviceaccount.com";
@@ -78,8 +77,8 @@ public static void getIdTokenUsingOAuth2(
7877
.build();
7978

8079
// Get the ID token.
81-
// Once you've obtained the ID token, use it to make an authenticated call
82-
// to the target audience.
80+
// Once you've obtained the ID token, you can use it to make an authenticated call to the
81+
// target audience.
8382
String idToken = idTokenCredentials.refreshAccessToken().getTokenValue();
8483
System.out.println("Generated ID token.");
8584
}

samples/snippets/src/main/java/IdTokenFromMetadataServer.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
3030
// TODO(Developer): Replace the below variables before running the code.
3131

3232
// The url or target audience to obtain the ID token for.
33-
String url = "http://www.abc.com";
33+
String url = "https://example.com";
3434

3535
getIdTokenFromMetadataServer(url);
3636
}
3737

38-
// Use the Google Cloud metadata server in the Cloud Run (or AppEngine or Kubernetes etc.,)
39-
// environment to create an identity token and add it to the HTTP request as part of an
40-
// Authorization header.
38+
// Use the Google Cloud metadata server to create an identity token and add it to the
39+
// HTTP request as part of an Authorization header.
4140
public static void getIdTokenFromMetadataServer(String url) throws IOException {
4241
// Construct the GoogleCredentials object which obtains the default configuration from your
4342
// working environment.
@@ -52,8 +51,8 @@ public static void getIdTokenFromMetadataServer(String url) throws IOException {
5251
.build();
5352

5453
// Get the ID token.
55-
// Once you've obtained the ID token, use it to make an authenticated call
56-
// to the target audience.
54+
// Once you've obtained the ID token, you can use it to make an authenticated call to the
55+
// target audience.
5756
String idToken = idTokenCredentials.refreshAccessToken().getTokenValue();
5857
System.out.println("Generated ID token.");
5958
}

samples/snippets/src/main/java/IdTokenFromServiceAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void main(String[] args)
4444
String jsonCredentialPath = "path-to-json-credential-file";
4545

4646
// The url or target audience to obtain the ID token for.
47-
String targetAudience = "http://www.abc.com";
47+
String targetAudience = "https://example.com";
4848

4949
getIdTokenFromServiceAccount(jsonCredentialPath, targetAudience);
5050
}

samples/snippets/src/main/java/VerifyGoogleIdToken.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ public static void main(String[] args) {
2626
// The Google ID token to verify.
2727
String idToken = "id-token";
2828

29-
// The service name for which the id token is requested. Service name refers to the
30-
// logical identifier of an API service, such as "pubsub.googleapis.com".
31-
String targetAudience = "pubsub.googleapis.com";
29+
// The service name for which the id token was requested.
30+
String targetAudience = "https://example.com";
3231

3332
// To verify id tokens, get the Json Web Key endpoint (jwk).
3433
// OpenID Connect allows the use of a "Discovery document," a JSON document found at a

samples/snippets/src/test/java/SnippetsIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ private static String getIdTokenFromServiceAccount(
9595

9696
@Test
9797
public void testIdTokenFromServiceAccount() throws IOException {
98-
IdTokenFromServiceAccount.getIdTokenFromServiceAccount(CREDENTIALS, "iap.googleapis.com");
98+
IdTokenFromServiceAccount.getIdTokenFromServiceAccount(CREDENTIALS, "https://example.com");
9999
assertThat(stdOut.toString()).contains("Generated ID token.");
100100
}
101101

102102
@Test
103103
public void testVerifyGoogleIdToken() throws IOException {
104-
String idToken = getIdTokenFromServiceAccount(CREDENTIALS, "iap.googleapis.com");
104+
String idToken = getIdTokenFromServiceAccount(CREDENTIALS, "https://example.com");
105105

106106
VerifyGoogleIdToken.verifyGoogleIdToken(
107-
idToken, "iap.googleapis.com", "https://www.googleapis.com/oauth2/v3/certs");
107+
idToken, "https://example.com", "https://www.googleapis.com/oauth2/v3/certs");
108108
}
109109

110110
@Test

0 commit comments

Comments
 (0)