@@ -36,6 +36,10 @@ export function escapeOData(value: string): string {
3636 return value . replace ( / ' / g, "''" ) ;
3737}
3838
39+ function normalizeOptionalTenantId ( value : unknown ) : string | undefined {
40+ return typeof value === "string" && value . trim ( ) ? value . trim ( ) : undefined ;
41+ }
42+
3943async function requestGraph ( params : {
4044 token : string ;
4145 path : string ;
@@ -214,11 +218,14 @@ export async function resolveGraphToken(
214218 if ( ! creds ) {
215219 throw new Error ( "MS Teams credentials missing" ) ;
216220 }
221+ const graphTenantId = normalizeOptionalTenantId ( msteamsCfg ?. graphTenantId ) ?? creds . tenantId ;
222+ const graphCreds =
223+ graphTenantId === creds . tenantId ? creds : { ...creds , tenantId : graphTenantId } ;
217224
218225 // Try delegated token if requested and configured
219226 if ( options ?. preferDelegated && msteamsCfg ?. delegatedAuth ?. enabled && creds . type === "secret" ) {
220227 const delegated = await resolveDelegatedAccessToken ( {
221- tenantId : creds . tenantId ,
228+ tenantId : graphTenantId ,
222229 clientId : creds . appId ,
223230 clientSecret : creds . appPassword ,
224231 } ) ;
@@ -228,7 +235,7 @@ export async function resolveGraphToken(
228235 // Fall through to app-only token
229236 }
230237
231- const { app } = await loadMSTeamsSdkWithAuth ( creds ) ;
238+ const { app } = await loadMSTeamsSdkWithAuth ( graphCreds ) ;
232239 const tokenProvider = createMSTeamsTokenProvider ( app ) ;
233240 const graphTokenValue = await tokenProvider . getAccessToken ( "https://graph.microsoft.com" ) ;
234241 const accessToken = readAccessToken ( graphTokenValue ) ;
0 commit comments