我正在尝试授权我的应用程序查询 Log Analytics API。作为 Microsoft 文档中描述的流程的一部分,我需要使用 Microsoft Entra ID OAuth2 流请求令牌。
我找不到错误,因为我正在使用 databricks 内的范围来访问 keyvault 内的秘密。 秘密的值属于服务主体。
我正在 databricks 中运行 jupyter 笔记本。这是代码(匿名):
url = "https://login.microsoftonline.com/xxxxxxx/oauth2/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
key = dbutils.secrets.get(scope="kv_XXX", key="XXXx")
payload = {"grant_type":"client_credentials",
"client_id": "2xxxxx",
"scope":"34xxxxxxx/.default",
"client_secret": key}
response = requests.post(url, headers=headers, data=payload)
print(response.text)
这是错误:
AADSTS7000215:提供的客户端密钥无效。对于添加到应用程序“xxxxx”的密钥,确保请求中发送的密钥是客户端密钥值,而不是客户端密钥 ID。
我尝试过其他身份验证方法,但错误仍然存在。
您可能提供的是秘密 ID,而不是秘密值。
您需要将下图中突出显示的密钥值添加到您的 Key Vault 密钥中。
因此,请验证您在 Key Vault 机密中输入的值以确保其正确。
import requests
url = "https://login.microsoftonline.com/<tenant_id>/oauth2/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
key = dbutils.secrets.get('tst_scope','cogsec')
payload = {"grant_type":"client_credentials",
"client_id": "<client_id>",
"scope":"<client_id>/.default",
"client_secret": key}
response = requests.post(url, headers=headers, data=payload)
print(response.text)
输出:
如果仍然出现错误,请检查是否传递了您已授予权限的正确应用程序的秘密值。