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

Angular 9中HTTP请求如何传递变量 为MSAL拦截器配置添加时间参数

Angular 9 MSAL拦截器动态添加时间参数实现方案

第一步:调整MSAL拦截器的资源匹配规则

MSAL拦截器的protectedResourceMap支持通配符前缀匹配,无需将固定时间参数写死在配置中,修改配置如下:

export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
  const protectedResourceMap = new Map<string, Array<string>>();
  // 通配符*匹配所有以该前缀开头的请求,所有带动态参数的同接口请求都会自动命中该权限配置
  protectedResourceMap.set('https://graph.microsoft.com/v1.0/communications/callRecords/getPstnCalls*', ['CallRecords.Read.All','CallRecord-PstnCalls.Read.All','CallRecords.Read.All']);

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

第二步:发起请求时动态拼接时间参数

该接口是微软Graph的OData格式接口,参数需要拼接在路径的括号中,使用模板字符串动态插入变量即可:

import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';

@Component({
  // 你的组件配置
})
export class YourComponent {
  constructor(private http: HttpClient) {}

  getPstnCalls(fromDate: string, toDate: string) {
    // 时间格式需符合ISO 8601标准,例如2024-01-01、2024-01-01T00:00:00Z
    const requestUrl = `https://graph.microsoft.com/v1.0/communications/callRecords/getPstnCalls(fromDateTime=${fromDate},toDateTime=${toDate})`;
    
    this.http.get(requestUrl).subscribe({
      next: (res) => {
        // 处理返回的通话记录数据
      },
      error: (err) => {
        // 处理请求异常
      }
    })
  }
}

注意事项

  • 时间参数必须符合微软Graph接口要求的ISO 8601格式,建议携带时区标识避免时间偏移问题
  • 若需要额外添加查询参数,可直接在路径后拼接,通配符匹配规则依然生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 08:27:00