当我调用business central api时,为什么总是得到“服务器拒绝了客户端凭据”?

2024-10-02 22:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的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('/') 

这是我对business central 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('/')

这就是我在调用/向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

这是business central的有效终结点列表:https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/endpoints-apis-for-dynamics


Tags: httpscomclienttokenapiidappdata
2条回答

2021-10-12更新:BC现在支持客户端凭据流

需要进行一些额外的设置。以下链接将引导您浏览:

https://www.kauffmann.nl/2021/07/06/service-to-service-authentication-in-business-central-18-3-how-to-set-up/

归结起来是:

  1. 在Azure Active Directory中注册外部应用程序
    • 在Azure中创建应用程序(特别注意重定向URI)
    • 设置所需的权限(“Dynamics 365 Business Central/API.ReadWrite.All”)
    • 制造秘密
  2. 在Business Central中创建外部应用程序帐户
    • 从您注册的Azure应用程序添加客户端ID
    • 添加权限(例如“D365基本”和“D365销售单据,编辑”)
    • 同意

此时,您可以通过以下方式获得令牌:

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中的外部应用程序绑定在一起,但没有激活

此处不支持客户端凭据流。Dynamics 365 BC支持的身份验证方法仅包括以下两个基于官方文档的选项:

  • 基础认证
  • AAD认证

如果希望使用不需要用户交互的方法调用D365 BC API,则应选择Basis Authentication

具体步骤如下:

  1. 要设置基本身份验证,请登录租户,然后在 搜索字段,输入用户,然后选择相关链接
  2. 选择要添加访问权限的用户,并在“用户卡”页面上,在 Web服务访问密钥字段,生成密钥
  3. 复制生成的密钥并将其用作用户名的密码

然后你可以引用Exploring the APIs with Postman and basic authentication

相关问题 更多 >