简体   繁体   English

IBM Cloud:API function 查找现有 IAM 访问令牌的帐户 ID?

[英]IBM Cloud: API function to find account ID for existing IAM access token?

In my script I have an IBM Cloud IAM bearer token (access token).在我的脚本中,我有一个 IBM Cloud IAM 持有者令牌(访问令牌)。 Some API functions require to pass in the account ID.一些 API 函数需要传入帐户 ID。 Which API functions would allow me to retrieve the related account ID?哪些 API 函数可以让我检索相关帐户 ID? I know that I could base64 decode the access token, but...我知道我可以 base64 解码访问令牌,但是......

docs: https://cloud.ibm.com/apidocs/iam-identity-token-api?code=python#get-api-keys-details-permissions文档: https://cloud.ibm.com/apidocs/iam-identity-token-api?code=python#get-api-keys-details-permissions

Python: Python:

iam_identity_service.get_api_keys_details( iam_api_key=apikey ).get_result() iam_identity_service.get_api_keys_details(iam_api_key=apikey).get_result()

There is an API function to retrieve the list of accounts and only needs a bearer token:有一个 API function 来检索帐户列表,只需要一个不记名令牌:

curl -X GET "https://accounts.cloud.ibm.com/v1/accounts" 
-H "Authorization: $IBMCLOUD_TOKEN" -H 'Content-Type: application/json'

The API can be seen in use with trace enabled during login using the CLI.可以看到 API 正在使用中,并在使用 CLI 登录期间启用了跟踪。 The service is described as Account Management Service in the CLI docs .该服务在 CLI 文档中被描述为 Account Management Service

I resorted to just decoding the bearer token:我只好解码承载令牌:

# use split and base64 to get to the content of the IAM token
def extractAccount(iam_token):
    data = iam_token.split('.')
    padded = data[1] + "="*divmod(len(data[1]),4)[1]
    jsondata = json.loads(base64.urlsafe_b64decode(padded))
    return jsondata

The account ID can then be retrieved by accessing the related field in the token payload:然后可以通过访问令牌有效负载中的相关字段来检索帐户 ID:

token_data=extractAccount(iam_token)
account_id=token_data["account"]["bss"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IBM Cloud:如何访问API进行计费和使用? - IBM Cloud: How to access the API for billing and usage? 将当前 URL 参数传递给 Cloud Function Python ZDB974238714CA8DE634A7CE1D08 上 Cloud - Pass current URL parameters to Cloud Function Python API on IBM Cloud 如何检索 id_token 以访问 Google Cloud Function? - How can I retrieve an id_token to access a Google Cloud Function? Cloud Function获取“ getIamPolicy”的访问令牌 - Cloud Function get access token for `getIamPolicy` Google Analytics Embed API服务帐户访问令牌 - Google Analytics Embed API Service Account Access Token 如何导入 Cloudant 模块以通过 IBM 云服务器少功能 python 3.9 操作来操作现有文档? - How to import Cloudant module to manipulate existing documents via IBM cloud server less function python 3.9 actions? GCP - Python 创建云函数调用 Gmail Api 存储帐户 - GCP - Python Create cloud function call Gmail Api Storage Account AWS IAM:将访问密钥ID从根更改为IAM - AWS IAM : change Access Key Id the from root to IAM "在 python 云函数中验证 JWT 访问令牌时遇到问题" - Trouble validating JWT access token in python cloud function Boto3:如何承担 IAM 角色以访问其他帐户 - Boto3: How to assume IAM Role to access other account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM