即使我获得令牌,Azure 应用程序也会未经授权

问题描述 投票:0回答:1

首先。我是天蓝色平台的超级新手。当我去的时候试图弄清楚如何解决这个问题。

private class TokenRequestTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... voids) {
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("client_id", "{CLIENT_ID}")
                .add("client_secret", "{CLIENT_SECRET}")
                .add("scope", "https://api.businesscentral.dynamics.com/v2.0/.default")
                .add("grant_type", "client_credentials")
                .build();
        Request request = new Request.Builder()
                .url("https://login.windows.net/{TENANT_ID}/oauth2/v2.0/token")
                .post(formBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                String responseHeader = response.headers().toString();         
                JSONObject jsonObject = new JSONObject(responseBody);
                String accessToken = jsonObject.getString("access_token");
                SharedPreferences._instance.setToken(accessToken);
                return accessToken;
            } else {
                return "Request failed: " + response.code();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Request failed: " + e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        // Handle the token or error response
        System.out.println(result);
    }
}

此代码用于测试目的,一旦我们使一切正常工作,我们将在将来进行更改。

此令牌请求任务能够检索令牌。但问题是当我们尝试调用自定义 api 时。我们收到 401,响应让我们知道凭据无效。

网址看起来像这样

https://api.businesscentral.dynamics.com/v2.0/{TENANT_ID}/Production/api/effekt/timeReport/v2.0/companies({COMPANY_ID})

我们创建的应用程序对应于CLIENT_ID和CLIENT_SECRET有 Permissions

有了所有这些权限,它仍然会返回 401。可能出什么问题了?有没有办法知道需要什么许可?或者我们需要做什么

解码后的令牌

{
  "typ": "JWT",
  "alg": "RS256",
  "x5t": "L1KfKFI_jnXbwWc22xZxw1sUHH0",
  "kid": "L1KfKFI_jnXbwWc22xZxw1sUHH0"
}.{
  "aud": "https://api.businesscentral.dynamics.com",
  "iss": "https://sts.windows.net/{TENANT_ID}",
 ...
  "roles": [
    "Automation.ReadWrite.All",
    "app_access",
    "AdminCenter.ReadWrite.All",
    "API.ReadWrite.All"
  ],
...

  "ver": "1.0"
}.[Signature]
azure
1个回答
0
投票

如果您未在 Dynamics 365 Business Central 门户中添加 Microsoft Entra ID 应用程序并在 D365 门户中授予权限,通常会发生此错误。

我创建了一个 Microsoft Entra ID 应用程序并授予了 API 权限:

enter image description here

现在,登录 Dynamics 365 Business Central Portal -> 单击搜索图标 -> 搜索 Microsoft Entra 应用程序 -> 新建

enter image description here

enter image description here

粘贴 Microsoft Entra Application ClientID 并将状态设置为“已启用”-> 单击“是”

enter image description here

enter image description here

在权限选项卡中授予完全访问权限权限:

enter image description here

现在,登录 Dynamics 365 Business Central 门户 -> 设置 -> 管理中心

enter image description here

并复制TenantID和环境名称:

enter image description here

我使用以下参数生成了访问令牌

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
client_secret:ClientSecret
grant_type:client_credentials
scope:https://api.businesscentral.dynamics.com/.default

enter image description here

我能够成功获得以下公司:

GET https://api.businesscentral.dynamics.com/v2.0/TenantID/EnvironmentName/api/v2.0/companies

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.