使用Azure Communication Services获取中继配置时遇服务不可用错误
问题分析与解决方案
问题重现
尝试通过Azure Communication Services创建用户身份并获取网络穿透中继配置,初始使用官方SDK代码:
const { CommunicationIdentityClient } = require("@azure/communication-identity"); const { CommunicationRelayClient } = require("@azure/communication-network-traversal"); const main = async () => { console.log("Azure Communication Services - Relay Token Quickstart") const connectionString = "CONNECTION STRING" // Instantiate the identity client const identityClient = new CommunicationIdentityClient(connectionString); let identityResponse = await identityClient.createUser(); console.log(`\nCreated an identity with ID: ${identityResponse.communicationUserId}`); const relayClient = new CommunicationRelayClient(connectionString); console.log("Getting relay configuration"); const config = await relayClient.getRelayConfiguration(identityResponse); console.log("RelayConfig", config); console.log("Printing entire thing ",config.iceServers); }; main().catch((error) => { console.log("Encountered and error"); console.log(error); })
运行后返回错误:
<h2>Our services aren't available right now</h2><p>We're working to restore all services as soon as possible. Please check back soon.</p>
参考社区方案修改为REST请求后,仍出现相同错误:
const { CommunicationIdentityClient } = require("@azure/communication-identity"); const axios = require('axios'); const main = async () => { console.log("Azure Communication Services - Relay Token Quickstart"); const connectionString = "CONNECTION STRING"; // Extract endpoint and access key from the connection string const endpoint = connectionString.match(/endpoint=(.*?);/)[1]; const accessKey = connectionString.match(/accesskey=(.*)/)[1]; // Instantiate the identity client const identityClient = new CommunicationIdentityClient(connectionString); try { let identityResponse = await identityClient.createUser(); console.log(`\nCreated an identity with ID: ${identityResponse.communicationUserId}`); // Issue an access token for the created user const tokenResponse = await identityClient.getToken(identityResponse, ["voip"]); const accessToken = tokenResponse.token; console.log(`\nIssued an access token: ${accessToken}`); // Construct the relay configuration request const relayConfigUrl = `${endpoint}/networkTraversal/:issueRelayConfiguration?api-version=2022-03-01-preview`; const response = await axios.post(relayConfigUrl, { id: identityResponse.communicationUserId }, { headers: { 'Authorization': `Bearer ${accessToken}`, 'Content-Type': 'application/json' } }); console.log("RelayConfig", response.data); console.log("Printing entire thing ", response.data.iceServers); } catch (error) { console.log("Encountered an error"); console.log(error.response ? error.response.data : error.message); } }; main().catch((error) => { console.log("Encountered an error"); console.log(error); });
可能的原因及解决步骤
1. 端点格式错误(REST请求核心问题)
修改后的REST请求端点存在格式错误:/:issueRelayConfiguration 是错误路径,正确的网络穿透API路径应为 /networkTraversal/relayConfigurations,同时需使用稳定版API版本(如2023-03-31),避免旧预览版的稳定性问题。
修正后的请求URL:
const relayConfigUrl = `${endpoint}/networkTraversal/relayConfigurations?api-version=2023-03-31`;
2. 权限范围不匹配
获取用户令牌时,仅设置voip权限不足以访问网络穿透服务,需添加network-traversal权限范围。
修正令牌获取代码:
const tokenResponse = await identityClient.getToken(identityResponse, ["network-traversal"]);
3. SDK版本兼容性(针对初始SDK代码)
若坚持使用官方SDK,需确保@azure/communication-network-traversal包为最新稳定版,旧版本可能存在端点配置错误。执行更新命令:
npm update @azure/communication-network-traversal @azure/communication-identity
4. 资源状态与区域检查
- 登录Azure门户确认Communication Services资源处于正常运行状态,无故障告警。
- 确保请求区域与资源部署区域一致,跨区域访问可能导致服务不可用。
修正后的完整代码(REST版本)
const { CommunicationIdentityClient } = require("@azure/communication-identity"); const axios = require('axios'); const main = async () => { console.log("Azure Communication Services - Relay Token Quickstart"); const connectionString = "CONNECTION STRING"; const identityClient = new CommunicationIdentityClient(connectionString); try { const identityResponse = await identityClient.createUser(); console.log(`\nCreated an identity with ID: ${identityResponse.communicationUserId}`); // 申请包含网络穿透权限的令牌 const tokenResponse = await identityClient.getToken(identityResponse, ["network-traversal"]); const accessToken = tokenResponse.token; console.log(`\nIssued an access token: ${accessToken}`); // 使用正确的API端点和稳定版API版本 const endpoint = connectionString.match(/endpoint=(.*?);/)[1]; const relayConfigUrl = `${endpoint}/networkTraversal/relayConfigurations?api-version=2023-03-31`; const response = await axios.post(relayConfigUrl, { communicationUserId: identityResponse.communicationUserId }, { headers: { 'Authorization': `Bearer ${accessToken}`, 'Content-Type': 'application/json' } }); console.log("RelayConfig", response.data); console.log("ICE Servers: ", response.data.iceServers); } catch (error) { console.log("Encountered an error"); console.log(error.response ? error.response.data : error.message); } }; main().catch((error) => { console.log("Encountered an error"); console.log(error); });
修正后的完整代码(SDK版本)
const { CommunicationIdentityClient } = require("@azure/communication-identity"); const { CommunicationRelayClient } = require("@azure/communication-network-traversal"); const main = async () => { console.log("Azure Communication Services - Relay Token Quickstart") const connectionString = "CONNECTION STRING"; const identityClient = new CommunicationIdentityClient(connectionString); const identityResponse = await identityClient.createUser(); console.log(`\nCreated an identity with ID: ${identityResponse.communicationUserId}`); // 确保SDK版本为最新,直接使用客户端获取中继配置 const relayClient = new CommunicationRelayClient(connectionString); console.log("Getting relay configuration"); const config = await relayClient.getRelayConfiguration(identityResponse); console.log("RelayConfig", config); console.log("ICE Servers: ", config.iceServers); }; main().catch((error) => { console.log("Encountered and error"); console.log(error); })
内容的提问来源于stack exchange,提问作者Hamza Khan
相关产品推荐
相关产品推荐

