forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_api_key.py
More file actions
56 lines (43 loc) · 1.86 KB
/
create_api_key.py
File metadata and controls
56 lines (43 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START apikeys_create_api_key]
from google.cloud import api_keys_v2
from google.cloud.api_keys_v2 import Key
def create_api_key(project_id: str, suffix: str) -> Key:
"""
Creates and restrict an API key. Add the suffix for uniqueness.
TODO(Developer):
1. Before running this sample,
set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc
2. Make sure you have the necessary permission to create API keys.
Args:
project_id: Google Cloud project id.
Returns:
response: Returns the created API Key.
"""
# Create the API Keys client.
client = api_keys_v2.ApiKeysClient()
key = api_keys_v2.Key()
key.display_name = f"My first API key - {suffix}"
# Initialize request and set arguments.
request = api_keys_v2.CreateKeyRequest()
request.parent = f"projects/{project_id}/locations/global"
request.key = key
# Make the request and wait for the operation to complete.
response = client.create_key(request=request).result()
print(f"Successfully created an API key: {response.name}")
# For authenticating with the API key, use the value in "response.key_string".
# To restrict the usage of this API key, use the value in "response.name".
return response
# [END apikeys_create_api_key]