簡體   English   中英

Rest API Google Cloud-創建/編輯/列出 API 密鑰

[英]Rest API Google Cloud- create/edit/list API Keys

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,您可以使用以下命令( “gcloud alpha services api-keys”文檔)創建 API 密鑰:

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

在仔細搜索谷歌的雲 文檔后,我沒有找到一種編程方式(示例 1),也沒有使用 cURL(示例 2)調用谷歌 API 來管理 API 密鑰。

以編程方式創建存儲桶的示例 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"

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 密鑰:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM