为什么我打电话给业务中心 api 总是得到 'The server has rejected the client credentials.'?
Why do I always get 'The server has rejected the client credentials.' when I call business central api?
这是我的 MSAL 身份验证:
@app.route('/get-microsoft-data', methods=('GET', 'POST'))
def get_microsoft_token():
public_app = ConfidentialClientApplication(
client_id="<client_id>", authority="https://login.microsoftonline.com/<tenant_id>",
client_credential="<client_secret>"
)
result = None
result = public_app.acquire_token_silent(["https://api.businesscentral.dynamics.com/.default"], account=None)
if not result:
print("No suitable token exists in cache. Let's get a new one from AAD.")
result = public_app.acquire_token_for_client(scopes=["https://api.businesscentral.dynamics.com/.default"])
if "access_token" in result:
global microsoft_token
microsoft_token = result["access_token"]
return redirect('/')
这是我给商务中心的电话api:
@app.route('/send-data-to-microsoft', methods=('GET', 'POST'))
def send_data_to_microsoft():
print(microsoft_token)
headers = {
"Authorization": "Bearer " + microsoft_token
}
r = requests.get("https://api.businesscentral.dynamics.com/v1.0/<tenant_domain>/sandbox/api/v1.0/companies", headers=headers)
print(r.json())
return redirect('/')
这是我调用 /send-data-to-microsoft 时遇到的错误:
{'error': {'code': 'Authentication_InvalidCredentials', 'message': 'The server has rejected the client credentials. CorrelationId: ff4d9d32-db03-4c2a-bf77-2e6186d4988c.'}}
这是我想要的文档:https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/api/dynamics_companies_get
这是业务中心的有效端点列表:https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/endpoints-apis-for-dynamics
此处不支持客户端凭据流。 Dynamics 365 BC支持的Authentication methods根据官方文档只有这2个选项:
- 基本身份验证
- AAD 身份验证
如果您想使用不需要用户交互的方法调用 D365 BC API,您应该选择 Basis Authentication。
操作步骤如下:
- 要设置基本身份验证,请登录您的租户,然后在
搜索字段,输入用户然后 select 相关 link.
- Select 要为其添加访问权限的用户,并在“用户卡”页面上,在
Web 服务访问密钥字段,生成一个密钥。
- 复制生成的密钥并将其用作用户名的密码。
然后你可以参考Exploring the APIs with Postman and basic authentication。
2021-10-12 更新:BC 现在支持客户端凭证流。
需要进行一些额外的设置。以下 link 将引导您完成它:
归结为:
- 在 Azure Active Directory 中注册外部应用程序
- 在 Azure 中创建应用程序(特别注意重定向 URI)
- 设置所需的权限(“Dynamics 365 Business Central / API.ReadWrite.All”)
- 创建一个秘密
- 在 Business Central 中创建外部应用程序帐户
- 从您注册的 Azure 应用中添加客户端 ID
- 添加权限(例如,“D365 BASIC”和“D365 SALES DOC,EDIT”)
- 同意
届时您可以获得一个令牌:
curl --location --request GET 'https://login.microsoftonline.com/<tenant>/oauth2/v2.0/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=<client>' \
--data-urlencode 'scope=https://api.businesscentral.dynamics.com/.default' \
--data-urlencode 'client_secret=<secret>'
并且客户查询工作为:
curl --location --request GET 'https://api.businesscentral.dynamics.com/v2.0/<tenant>/<env>/api/v2.0/companies(<company id>)/customers' \
--header 'Authorization: Bearer XYZ...'
顺便说一句,我遇到了同样的 Authentication_InvalidCredentials
错误,结果证明它与 BC 中的外部应用程序绑定未激活。
这是我的 MSAL 身份验证:
@app.route('/get-microsoft-data', methods=('GET', 'POST'))
def get_microsoft_token():
public_app = ConfidentialClientApplication(
client_id="<client_id>", authority="https://login.microsoftonline.com/<tenant_id>",
client_credential="<client_secret>"
)
result = None
result = public_app.acquire_token_silent(["https://api.businesscentral.dynamics.com/.default"], account=None)
if not result:
print("No suitable token exists in cache. Let's get a new one from AAD.")
result = public_app.acquire_token_for_client(scopes=["https://api.businesscentral.dynamics.com/.default"])
if "access_token" in result:
global microsoft_token
microsoft_token = result["access_token"]
return redirect('/')
这是我给商务中心的电话api:
@app.route('/send-data-to-microsoft', methods=('GET', 'POST'))
def send_data_to_microsoft():
print(microsoft_token)
headers = {
"Authorization": "Bearer " + microsoft_token
}
r = requests.get("https://api.businesscentral.dynamics.com/v1.0/<tenant_domain>/sandbox/api/v1.0/companies", headers=headers)
print(r.json())
return redirect('/')
这是我调用 /send-data-to-microsoft 时遇到的错误:
{'error': {'code': 'Authentication_InvalidCredentials', 'message': 'The server has rejected the client credentials. CorrelationId: ff4d9d32-db03-4c2a-bf77-2e6186d4988c.'}}
这是我想要的文档:https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/api/dynamics_companies_get
这是业务中心的有效端点列表:https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/endpoints-apis-for-dynamics
此处不支持客户端凭据流。 Dynamics 365 BC支持的Authentication methods根据官方文档只有这2个选项:
- 基本身份验证
- AAD 身份验证
如果您想使用不需要用户交互的方法调用 D365 BC API,您应该选择 Basis Authentication。
操作步骤如下:
- 要设置基本身份验证,请登录您的租户,然后在 搜索字段,输入用户然后 select 相关 link.
- Select 要为其添加访问权限的用户,并在“用户卡”页面上,在 Web 服务访问密钥字段,生成一个密钥。
- 复制生成的密钥并将其用作用户名的密码。
然后你可以参考Exploring the APIs with Postman and basic authentication。
2021-10-12 更新:BC 现在支持客户端凭证流。
需要进行一些额外的设置。以下 link 将引导您完成它:
归结为:
- 在 Azure Active Directory 中注册外部应用程序
- 在 Azure 中创建应用程序(特别注意重定向 URI)
- 设置所需的权限(“Dynamics 365 Business Central / API.ReadWrite.All”)
- 创建一个秘密
- 在 Business Central 中创建外部应用程序帐户
- 从您注册的 Azure 应用中添加客户端 ID
- 添加权限(例如,“D365 BASIC”和“D365 SALES DOC,EDIT”)
- 同意
届时您可以获得一个令牌:
curl --location --request GET 'https://login.microsoftonline.com/<tenant>/oauth2/v2.0/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=<client>' \
--data-urlencode 'scope=https://api.businesscentral.dynamics.com/.default' \
--data-urlencode 'client_secret=<secret>'
并且客户查询工作为:
curl --location --request GET 'https://api.businesscentral.dynamics.com/v2.0/<tenant>/<env>/api/v2.0/companies(<company id>)/customers' \
--header 'Authorization: Bearer XYZ...'
顺便说一句,我遇到了同样的 Authentication_InvalidCredentials
错误,结果证明它与 BC 中的外部应用程序绑定未激活。