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

从客户端调用Microsoft Graph转换内容端点失败求助

Fixing CORS Issue When Fetching Converted PDFs via Microsoft Graph API

Hey there, I’ve run into this exact problem before with Microsoft Graph’s PDF conversion endpoint, so let me break down what’s happening and how to fix it.

The Root Cause

The issue boils down to CORS restrictions: when you get the 302 redirect from the Graph API, the target URL (westeurope1-mediap.svc.ms/transform/pdf) doesn’t support cross-origin requests. Browsers send an OPTIONS preflight check for frontend AJAX calls, and since this endpoint doesn’t handle that request (returning a 404), your JS call fails. Directly opening the URL works because that’s a simple GET request without preflight checks.

Practical Solutions

The most reliable fix is to route the request through your own backend server—server-side requests aren’t bound by CORS rules:

  • Your frontend sends either the initial Graph API request or just the redirected location URL to your backend.
  • Your backend makes a GET request to the converted PDF URL, preserving any auth tokens from the docid parameter.
  • Your backend streams or returns the PDF content back to your frontend.

Here’s a quick Node.js/Express example to illustrate:

const express = require('express');
const axios = require('axios');
const app = express();

app.get('/proxy-pdf', async (req, res) => {
  try {
    const pdfUrl = req.query.targetUrl; // Pass the redirected location URL here
    const pdfResponse = await axios.get(pdfUrl, { responseType: 'stream' });
    
    res.setHeader('Content-Type', 'application/pdf');
    pdfResponse.data.pipe(res);
  } catch (err) {
    res.status(err.response?.status || 500).send('Failed to retrieve PDF');
  }
});

app.listen(3000, () => console.log('Proxy server running on port 3000'));

2. Client-Side Conversion (Alternative)

If a backend proxy isn’t feasible, you could fetch the original file (e.g., your XLSX) via Graph API first, then use frontend libraries like xlsx and pdfmake to convert it to PDF client-side. Note that this might not preserve formatting as accurately as Graph’s conversion, and it’s more resource-heavy for end users.

Key Notes

  • Don’t rely on browser CORS extensions for production—they’re unsafe and won’t work for your end users.
  • Make sure your backend passes along the tempauth token included in the docid parameter of the redirected URL, as it’s required to access the converted PDF.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:49:51