Android Management API注册令牌二维码生成404错误排查求助
Android Management API创建注册令牌时返回404错误
我用Node.js基于Google Android Management API开发的移动设备管理(MDM)应用此前运行正常,但当前创建带二维码的注册令牌时出现404未找到错误。
相关代码
const createEnrollmentToken = async (req,res,policyId) => { try { if (global.collectionExists) { console.log(`Collection already exists. Data will not be stored.`); } else { // Qr Code Exapraition time setting for 1000 years const durationInYears = 1000; const secondsInYear = 365.25 * 24 * 60 * 60; // 365.25 days in a year to account for leap years const durationInSeconds = durationInYears * secondsInYear; const enrollmentToken = await androidManagementClient.enterprises.enrollmentTokens.create({ parent: global.enterpriseName, requestBody: { policyName: policyId, duration: `${durationInSeconds}s`, } }); console.log('Enrollment token created successfully:', enrollmentToken.data); try { /* qr image */ const policies = await PoliciesDataModel.find({ username: global.adminUserName, "Polices.policyName": req.body.policyId }); if (policies && policies.length > 0) { const image = { 'cht': 'qr', //cht represents the type of the chart or image, and its value is set to 'qr'. 'chs': '320x320', //chs specifies the size of the chart or image, and its value is set to '500x500', indicating a width and height of 500 pixels each. 'chl': enrollmentToken.data.qrCode //chl is the URL or data for the chart or image, and its value is taken from enrollmentToken.data.qrCode. }; console.log(enrollmentToken.data.qrCode ) const qrcodeImgUrl = 'https://chart.googleapis.com/chart?' + querystring.stringify(image); console.log('qrcodeImgUrl : ', qrcodeImgUrl) const qrDoc = { QrcodeImg: qrcodeImgUrl, expiredDateTime: enrollmentToken.data.expirationTimestamp }; } else { // If no policies were found, send a not found response console.log('No policies found for the given email and policy name.'); await res.redirect('/provision-Template'); } } catch (error) { console.log("Update Error", error) } // expiredDateTime: enrollmentToken.data.expirationTimestamp await res.redirect('/provision-Template'); } } catch (error) { console.error('Error creating enrollment token:', error.message); } };
错误截图

解决方案
- 校验
parent参数格式:global.enterpriseName必须严格遵循enterprises/[ENTERPRISE_ID]的格式,确认企业ID是否正确,可通过Google Cloud控制台核对。 - 确认
policyId有效性:确保policyId是已存在的策略完整名称,格式为enterprises/[ENTERPRISE_ID]/policies/[POLICY_NAME],避免策略不存在或ID拼写错误。 - 检查API权限与版本:确认Android Management API客户端已配置正确权限,同时更新客户端库到最新版本,避免因版本兼容问题导致请求失败。
- 修正令牌有效期设置:代码中设置的1000年有效期远超API允许的最大时长(1年,即31536000秒),过长有效期会被API拒绝。将
durationInYears改为1后重新测试。 - 排查参数传递问题:确认
req.body.policyId是否正确传递到函数中,避免参数缺失导致后续策略查找失败。
内容的提问来源于stack exchange,提问作者maranR
相关产品推荐
相关产品推荐

