Rest API Google Cloud-创建/编辑/列出API密钥 [英] Rest API Google Cloud- create/edit/list API Keys
问题描述
我正在尝试创建一个API来从VM实例内部管理API密钥,因此我正在寻找一种编程方式和/或使用Google REST APIS方式创建,删除和列出Google Cloud API管理凭据,尤其是API密钥.
I'm trying to create an API to manage API Keys from inside a VM instance, so I'm looking for a programmatic and/or using Google REST APIS way to create, delete and list Google Cloud API Management Credentials, especially API KEYs.
使用Google Cloud SDK,您可以使用以下命令创建密钥和API密钥("gcloud alpha服务api-keys"文档):
With Google Cloud SDK you can create and API keys with the following command("gcloud alpha services api-keys" docs):
$ gcloud alpha services api-keys create --display-name="test name"
在仔细搜索Google的云后文档我还没有找到编程的方法(示例1),也没有使用cURL(示例2)调用Google API来管理API密钥.
After carefully searching through the Google's Cloud documentation I haven't found a way of programmatic(example 1) nor using a cURL(example 2) to call Google API to manage API Keys.
以编程方式创建存储桶的示例1:
Example 1 of creating bucket in a programmatic way:
from google.cloud import storage
def create_bucket_class_location(bucket_name):
"""Create a new bucket in specific location with storage class"""
# bucket_name = "your-new-bucket-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
bucket.storage_class = "COLDLINE"
new_bucket = storage_client.create_bucket(bucket, location="us")
print(
"Created bucket {} in {} with storage class {}".format(
new_bucket.name, new_bucket.location, new_bucket.storage_class
)
)
return new_bucket
使用REST API创建存储桶的示例2:
Example 2 of creating bucket using REST APIs:
curl -X POST --data-binary @create-bucket.json \
-H "Authorization: Bearer OAUTH2_TOKEN" \
-H "Content-Type: application/json" \
"https://storage.googleapis.com/storage/v1/b?project=PROJECT_ID"
推荐答案
AFAIK,API-KEYS REST文档尚未发布.
AFAIK, API-KEYS REST documentation has not been published yet.
免责声明: alpha
状态API可能会发生变化.您的代码将来可能会中断.
Disclaimer: alpha
status APIs are subject to change. Your code might break in the future.
我建议创建一个新的Google Cloud Project进行测试.
I recommend creating a new Google Cloud Project to do your testing in.
以下示例适用于Windows.对于Linux和macOS,这些概念是相同的.
The following examples are for Windows. The concepts are the same for Linux and macOS.
我们需要几个变量设置:
We need a couple of variables setup:
# Your Project ID
set GCP_PROJECT=my-project-123456
# Google OAuth Access Token
# Fancy DOS batch stuff to fetch a token
call gcloud auth print-access-token > token
set /p GCP_TOKEN=<token
del token
示例1:创建API密钥:
set URL=https://apikeys.googleapis.com/v2alpha1/projects/%GCP_PROJECT%/keys?alt=json
curl -X POST %URL% ^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer %GCP_TOKEN%" ^
-d "{\"displayName\": \"test key\", \"restrictions\": {}}"
示例2-列出API密钥:
curl https://apikeys.googleapis.com/v2alpha1/projects/%GCP_PROJECT%/keys?alt=json ^
-H "Accept: application/json" ^
-H "Authorization: Bearer %GCP_TOKEN%"
示例#3-删除和API密钥
注意:您将需要KEY名称.使用列表示例.使用列表json name
键的最后一部分.
Note: You will need the KEY name. Use the list example. Use the last part of the list json name
key.
set URL=https://apikeys.googleapis.com/v2alpha1/projects/%GCP_PROJECT%/keys
set KEY=2be9ee20-955c-4405-ac0c-e9f8ae1a3839
curl -X DELETE %URL%/%KEY% ^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer %GCP_TOKEN%"
附加说明:
有一个 v2beta1
API.我尚未对此版本进行测试.
There is a v2beta1
API. I have not tested with this version.
示例端点:
https://apikeys.googleapis.com/v2beta1/projects/development-219304/keys
这篇关于Rest API Google Cloud-创建/编辑/列出API密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!