1+ /*
2+ * Copyright 2022 Google Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ // [START auth_cloud_restrict_api_key_android]
18+
19+ import com .google .api .apikeys .v2 .AndroidApplication ;
20+ import com .google .api .apikeys .v2 .AndroidKeyRestrictions ;
21+ import com .google .api .apikeys .v2 .ApiKeysClient ;
22+ import com .google .api .apikeys .v2 .Key ;
23+ import com .google .api .apikeys .v2 .Restrictions ;
24+ import com .google .api .apikeys .v2 .UpdateKeyRequest ;
25+ import com .google .protobuf .FieldMask ;
26+ import java .io .IOException ;
27+ import java .util .concurrent .ExecutionException ;
28+ import java .util .concurrent .TimeUnit ;
29+ import java .util .concurrent .TimeoutException ;
30+
31+ public class RestrictApiKeyAndroid {
32+
33+ public static void main (String [] args )
34+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
35+ // TODO(Developer): Before running this sample,
36+ // 1. Replace the variable(s) below.
37+ String projectId = "GOOGLE_CLOUD_PROJECT_ID" ;
38+
39+ // ID of the key to restrict. This ID is auto-created during key creation.
40+ // This is different from the key string. To obtain the key_id,
41+ // you can also use the lookup api: client.lookupKey()
42+ String keyId = "key_id" ;
43+
44+ restrictApiKeyAndroid (projectId , keyId );
45+ }
46+
47+ // Restricts an API key based on android applications.
48+ // Specifies the Android application that can use the key.
49+ public static Key restrictApiKeyAndroid (String projectId , String keyId )
50+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
51+ // Initialize client that will be used to send requests. This client only needs to be created
52+ // once, and can be reused for multiple requests. After completing all of your requests, call
53+ // the `apiKeysClient.close()` method on the client to safely
54+ // clean up any remaining background resources.
55+ try (ApiKeysClient apiKeysClient = ApiKeysClient .create ()) {
56+
57+ // Restrict the API key usage by specifying the allowed android applications.
58+ Restrictions restrictions = Restrictions .newBuilder ()
59+ .setAndroidKeyRestrictions (AndroidKeyRestrictions .newBuilder ()
60+ .addAllowedApplications (AndroidApplication .newBuilder ()
61+ // Specify the android application's package name and SHA1 fingerprint.
62+ .setPackageName ("com.google.appname" )
63+ .setSha1Fingerprint ("0873D391E987982FBBD30873D391E987982FBBD3" )
64+ .build ())
65+ .build ())
66+ .build ();
67+
68+ Key key = Key .newBuilder ()
69+ .setName (String .format ("projects/%s/locations/global/keys/%s" , projectId , keyId ))
70+ // Set the restriction(s).
71+ // For more information on API key restriction, see:
72+ // https://cloud.google.com/docs/authentication/api-keys
73+ .setRestrictions (restrictions )
74+ .build ();
75+
76+ // Initialize request and set arguments.
77+ UpdateKeyRequest updateKeyRequest = UpdateKeyRequest .newBuilder ()
78+ .setKey (key )
79+ .setUpdateMask (FieldMask .newBuilder ().addPaths ("restrictions" ).build ())
80+ .build ();
81+
82+ // Make the request and wait for the operation to complete.
83+ Key result = apiKeysClient .updateKeyAsync (updateKeyRequest ).get (3 , TimeUnit .MINUTES );
84+
85+ // For authenticating with the API key, use the value in "result.getKeyString()".
86+ System .out .printf ("Successfully updated the API key: %s" , result .getName ());
87+ return result ;
88+ }
89+ }
90+ }
91+ // [END auth_cloud_restrict_api_key_android]
0 commit comments