範例:AgentCore CLI 建立的預設閘道和目標的授權
如果您使用 AgentCore CLI 建立閘道和 Lambda 目標,CLI 會自動為您設定 JWT 型傳入授權和 IAM 型傳出授權。
如果您使用 agentcore invoke 叫用閘道,CLI 會自動處理身分驗證。只有在您想要以程式設計方式叫用閘道 (例如,使用 AWS SDK 或 curl ) 時,才需要以下步驟。
對於程式設計存取,您將使用下列項目授權 :
您可以在 Amazon Cognito 的協助下,透過收集下列資訊,取得 AgentCore CLI 為閘道建立的存取字符:
首先,使用下列其中一種方法收集這些值。選取下列其中一種方法:
範例
- Console
-
-
取得字符端點:
-
取得用戶端 ID 和用戶端秘密 :
- AWS CLI
-
-
在終端機中執行 AgentCore get-gateway命令,並在--gateway-identifier引數中指定閘道 ID,如下列範例所示:
aws bedrock-agentcore-control get-gateway --gateway-identifier my-gateway-id
-
從回應中,記下 authorizerConfiguration 欄位discoveryUrl的 :
-
${UserPoolId} 從 URL 存放 。格式應為 https://cognito-idp.${Region}.amazonaws.com/${UserPoolId}/.well-known/openid-configuration。
-
存放allowedClients值。這些是可存取字符端點的用戶端 IDs。
-
導覽至瀏覽器discoveryUrl中的 ,並存放 token_endpoint值。
-
在終端機中執行 Amazon Cognito describe-user-pool-client命令,並在--user-pool-id引數中指定使用者集區 ID,並在 --client-id 欄位中指定允許的用戶端 ID,如下列範例所示:
aws cognito-idp describe-user-pool-client --user-pool-id my-user-pool-id --client-id my-client-id
-
存放 ClientSecret值。
- AWS Python SDK (Boto3)
-
-
執行下列 Python 程式碼,將字符端點、用戶端 ID 和用戶端秘密儲存為變數:
import requests
import boto3
# Initialize AWS clients
agentcore_client = boto3.client("bedrock-agentcore-control")
cognito_client = boto3.client("cognito-idp")
# Replace with your actual gateway ID
gateway_id = "my-gateway-id"
# Get discovery URL and first allowed client
auth_config = agentcore_client.get_gateway(gatewayIdentifier=gateway_id)["authorizerConfiguration"]["customJWTAuthorizer"]
discovery_url, client_id = auth_config["discoveryUrl"], auth_config["allowedClients"][0]
# Get token endpoint from discovery URL
discovery_url_json = requests.get(discovery_url).json()
token_endpoint, user_pool_id = discovery_url_json["token_endpoint"], discovery_url_json["issuer"].split("/")[-1]
# Get client secret
client_secret = cognito_client.describe_user_pool_client(
UserPoolId=user_pool_id,
ClientId=client_id
)["UserPoolClient"]["ClientSecret"]
其次,使用您收集的值從字符端點存取字符。選取下列其中一種方法:
範例
- curl
-
-
在終端機中執行下列命令,取代字符端點、用戶端 ID 和用戶端秘密值。
curl --http1.1 -X POST ${TokenEndpoint} \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=${ClientId}&client_secret=${ClientSecret}"
字符位於回應的 access_token 欄位中,而 token_type 欄位應指定 Bearer。
- Python requests package
-
-
執行下列 Python 程式碼以取得您的存取權杖。此程式碼假設您存放上一個步驟的 token_endpoint 、 client_id 和 client_secret值:
import requests
import json
def get_access_token(token_endpoint, client_id, client_secret):
headers={
"Content-Type": "application/x-www-form-urlencoded"
}
payload={
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret
}
response = requests.post(token_endpoint, headers=headers, data=payload)
return response.json()
# Replace the argument values as necessary, if you didn't previously store them as these variables
access_token_response = get_access_token(
token_endpoint=token_endpoint,
client_id=client_id,
client_secret=client_secret
)
access_token = access_token_response["access_token"]