You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure APIM中集成Microsoft Graph API及相关技术疑问

问题解答

一、将指定Microsoft Graph API集成到APIM服务

核心步骤

  1. 在APIM中创建新API,将后端地址设为 https://graph.microsoft.com/v1.0
  2. 添加GET操作,路径配置为 /servicePrincipals/,并设置查询参数:
    • $filter:值为 displayName eq 'Demo App'
    • $select:值为 appId,appDisplayName
      也可以直接将参数拼接在后端URL中,避免手动配置参数
  3. 配置身份验证:
    • 给APIM的系统/用户分配托管身份,在Azure AD中授予该身份 Application.Read.All 的Microsoft Graph API权限
    • 在API或操作的入站策略中添加托管身份验证逻辑,自动获取并携带令牌

关键策略示例

<authentication-managed-identity resource="https://graph.microsoft.com" output-token-variable-name="msGraphToken" ignore-error="false" />
<set-header name="Authorization" exists-action="override">
    <value>Bearer @(context.Variables.GetValueOrDefault<string>("msGraphToken"))</value>
</set-header>

二、APIM创建Mock API的实现方式

1. 直接通过响应策略返回模拟数据

在目标API操作的出站策略中添加return-response,自定义响应内容:

<outbound>
    <base />
    <return-response>
        <set-status code="200" reason="OK" />
        <set-header name="Content-Type" exists-action="override">
            <value>application/json</value>
        </set-header>
        <set-body>@{
            return "{
                \"value\": [
                    {
                        \"appId\": \"mock-abc-123\",
                        \"appDisplayName\": \"Demo App\"
                    }
                ]
            }";
        }</set-body>
    </return-response>
</outbound>

2. 利用APIM门户Mock开关

进入目标API操作页面,开启「Mock」开关,直接配置模拟响应的状态码、正文内容,支持基于OpenAPI定义自动生成符合规范的模拟数据。

3. 基于OpenAPI定义生成Mock

如果已有OpenAPI规范,导入到APIM时可直接启用Mock功能,APIM会自动生成匹配接口结构的模拟响应。

三、rewrite-uri策略与令牌传递问题

  • rewrite-uri无法生成令牌:该策略仅用于修改请求URI路径,不具备OAuth令牌生成能力。要获取令牌并传递给send-request,需使用专门的身份验证策略。

简便集成方案(优先推荐)

使用托管身份自动处理令牌的获取与刷新:

<inbound>
    <!-- 获取Graph API访问令牌 -->
    <authentication-managed-identity resource="https://graph.microsoft.com" output-token-variable-name="msGraphToken" ignore-error="false" />
    <!-- 调用指定Graph API并存储响应 -->
    <send-request mode="new" response-variable-name="graphResponse" timeout="20" ignore-error="false">
        <set-url>https://graph.microsoft.com/v1.0/servicePrincipals/?$filter=displayName eq 'Demo App'&amp;$select=appId,appDisplayName</set-url>
        <set-method>GET</set-method>
        <set-header name="Authorization" exists-action="override">
            <value>Bearer @(context.Variables.GetValueOrDefault<string>("msGraphToken"))</value>
        </set-header>
    </send-request>
    <!-- 将Graph API响应返回给客户端 -->
    <return-response>
        <set-status code="200" reason="OK" />
        <set-body>@(((IResponse)context.Variables["graphResponse"]).Body.As<string>())</set-body>
    </return-response>
</inbound>

内容的提问来源于stack exchange,提问作者Vara Lakshmi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 11:44:52