AADSTS65001:用户或管理员尚未同意使用 ID 为 <app-id> 的应用程序

Haj*_*jar 17 flask oauth-2.0 azure-active-directory azure-ad-msal angular

我正在开发一个使用 Microsoft 的 OAuth2(代表用户流程)的 Angular + Flask 应用程序。我试图从后端调用 API,但出现异常。

这是以下配置app.module.ts

export function MSALInstanceFactory(): IPublicClientApplication {
  return new PublicClientApplication({
    auth: {
      clientId: '<application_id_of_spa>',
      authority: 'https://login.microsoftonline.com/organizations',
      redirectUri: 'http://localhost:4200/'
    },
    cache: {
      cacheLocation: BrowserCacheLocation.LocalStorage,
      storeAuthStateInCookie: isIE,
    },
    system: {
      loggerOptions: {
        loggerCallback,
        logLevel: LogLevel.Info,
        piiLoggingEnabled: false
      }
    }
  });
}

export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
  const protectedResourceMap = new Map<string, Array<string>>();
  protectedResourceMap.set('https://graph.microsoft.com/v1.0/me', ['user.read']);
  protectedResourceMap.set('https://api.powerbi.com/v1.0/myorg/', ['https://analysis.windows.net/powerbi/api/.default']);
  protectedResourceMap.set('http://localhost:5000/api/v1.0/get_workspaces',['api://<application_id_of_webapi>/.default'])

  return {
    interactionType: InteractionType.Popup,
    protectedResourceMap
  };
}

export function MSALGuardConfigFactory(): MsalGuardConfiguration {
  return { 
    interactionType: InteractionType.Popup,
    authRequest: {
      scopes: ['api://<application_id_of_webapi>/.default'],
    },
  };
}
Run Code Online (Sandbox Code Playgroud)

然后我使用acquireTokenPopupmsal 函数来获取访问令牌。

然后我像这样调用我的后端 API:

this.http.get('http://localhost:5000/api/v1.0/get_workspaces')
Run Code Online (Sandbox Code Playgroud)

我的 Flask Web API:

@app.route('/api/v1.0/get_workspaces', methods=['GET'])
def get():

        current_access_token = request.headers.get("Authorization", None)

        msal_client = msal.ConfidentialClientApplication(
            client_id=app.config['CLIENT_ID'],
            authority=app.config['AUTHORITY'],
            client_credential=app.config['CLIENT_SECRET'])

        # acquire token on behalf of the user that called this API
        arm_resource_access_token = msal_client.acquire_token_on_behalf_of(
            user_assertion=current_access_token.split(' ')[1],
            scopes=app.config['SCOPE']
        )
        print( arm_resource_access_token) /////////////////// ******* I'm getting the error here

        headers = {
            'Authorization': arm_resource_access_token['token_type'] + ' ' + arm_resource_access_token['access_token']}

        workspaces= requests.get(app.config['ENDPOINT'] + 'workspaces', headers = headers).json()
        print(workspaces)
        return jsonify(workspaces)
Run Code Online (Sandbox Code Playgroud)

在我的角度控制台中,我得到了这个:

图像

在我的 Flask 终端中我得到这个:

export function MSALInstanceFactory(): IPublicClientApplication {
  return new PublicClientApplication({
    auth: {
      clientId: '<application_id_of_spa>',
      authority: 'https://login.microsoftonline.com/organizations',
      redirectUri: 'http://localhost:4200/'
    },
    cache: {
      cacheLocation: BrowserCacheLocation.LocalStorage,
      storeAuthStateInCookie: isIE,
    },
    system: {
      loggerOptions: {
        loggerCallback,
        logLevel: LogLevel.Info,
        piiLoggingEnabled: false
      }
    }
  });
}

export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
  const protectedResourceMap = new Map<string, Array<string>>();
  protectedResourceMap.set('https://graph.microsoft.com/v1.0/me', ['user.read']);
  protectedResourceMap.set('https://api.powerbi.com/v1.0/myorg/', ['https://analysis.windows.net/powerbi/api/.default']);
  protectedResourceMap.set('http://localhost:5000/api/v1.0/get_workspaces',['api://<application_id_of_webapi>/.default'])

  return {
    interactionType: InteractionType.Popup,
    protectedResourceMap
  };
}

export function MSALGuardConfigFactory(): MsalGuardConfiguration {
  return { 
    interactionType: InteractionType.Popup,
    authRequest: {
      scopes: ['api://<application_id_of_webapi>/.default'],
    },
  };
}
Run Code Online (Sandbox Code Playgroud)

在Azure门户中,我注册了spa和web API:

图像

我在后端公开了 API,并将其添加到我的前端注册中。

我将我的 spa app_id 添加到授权客户端应用程序上。

Sri*_*evi 20

AADSTS65001:用户或管理员尚未同意使用该应用程序

当您在检索访问令牌时错过了对添加的范围授予管理员同意时,通常会发生此错误。

要解决该错误,请检查您是否公开了 API,如下所示:

转到 Azure 门户 -> Azure Active Directory -> 应用程序注册 -> 你的应用程序 -> 公开 API

在此输入图像描述

公开 API 后,请确保授予API permissions它,如下所示:

转到 Azure 门户 -> Azure Active Directory -> 应用程序注册 -> 您的应用程序 -> API 权限 -> 添加权限 -> 我的 API -> 您的 API

在此输入图像描述

  • 添加 API 权限后,如果需要,请确保授予管理员同意。

  • 当您尝试获取访问令牌时,请检查您是否启用了以下选项:

转到 Azure 门户 -> Azure Active Directory -> 应用程序注册 -> 您的应用程序 -> 身份验证

在此输入图像描述

如果错误仍然存​​在,请检查以下链接:

Azure Active Directory - InteractionRequiredAuthError:AADSTS65001:用户或管理员尚未同意使用具有ID的应用程序 - VoidCC

.net - 代表用户获取令牌时出现“AADSTS65001:用户或管理员未同意使用该应用程序” - Thinbug

更新:

正如您在评论中提到的,请确保将您的客户端应用程序添加到已知客户端应用程序列表中