【问题标题】:Rest API Google Cloud- create/edit/list API KeysRest API Google Cloud - 创建/编辑/列出 API 密钥
【发布时间】:2021-04-24 00:18:13
【问题描述】:

我正在尝试创建一个 API 以从 VM 实例内部管理 API 密钥,因此我正在寻找一种编程和/或使用 Google REST APIS 方式来创建、删除和列出 Google Cloud API 管理凭据,尤其是API 密钥。

使用 Google Cloud SDK,您可以使用以下命令 ("gcloud alpha services api-keys" docs) 创建 API 密钥:

$ gcloud alpha services api-keys create --display-name="test name"

通过谷歌云documentation仔细搜索后,我没有找到一种编程方式(示例1),也没有找到使用cURL(示例2)调用Google API来管理API密钥的方式。

以编程方式创建bucket的示例1:

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:

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"

【问题讨论】:

    标签: python google-cloud-platform google-api


    【解决方案1】:

    AFAIK,API-KEYS REST 文档尚未发布。

    免责声明:alpha 状态 API 可能会发生变化。您的代码将来可能会损坏。

    我建议创建一个新的 Google Cloud 项目来进行测试。

    以下示例适用于 Windows。 Linux 和 macOS 的概念相同。

    我们需要设置几个变量:

    # 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 KEY:

    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

    注意:您将需要 KEY 名称。使用列表示例。使用列表的最后一部分 json name 键。

    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。我没有用这个版本测试过。

    示例端点:

    https://apikeys.googleapis.com/v2beta1/projects/development-219304/keys
    

    【讨论】:

    • 我建议您谨慎使用此 API。它已经在 1 年多前处于公测阶段,现在您只能使用这个旧的 gcloud CLI alpha 命令访问它,并且您在文档中找不到关于此的参考。关于 PM,没有人能够告诉我这个 API 的状态。它可能会在没有警告或公告的情况下消失一天!
    • @guillaumeblaquiere - 有一个 beta api 可用。这往往比 alpha 更稳定,并表明 Google 打算发布。但是,您的 cmets 很聪明。仅使用已发布的 API。
    猜你喜欢
    • 2020-10-24
    • 2022-07-28
    • 2021-01-31
    • 2019-02-11
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多