React Native调用Google My Business API报404 Method not found错误
报错原因排查
- API版本已废弃:Google My Business v4版本已于2022年全面下线,旧接口根地址
https://mybusiness.googleapis.com/v4/已停止服务,直接请求会返回404未找到错误。 - 接口路径不匹配:新版本接口的根域名和路径已更新,旧的路径规则不再适用。
- 账号ID参数错误:你传入的
userId如果是普通Google账号ID,而非Google My Business专属的、带accounts/前缀的完整账号ID,会导致请求路径不合法触发404。
解决方案
- 替换为新版本接口地址:将请求地址更换为Google My Business Business Information API v1的定位列表接口:
其中https://mybusinessbusinessinformation.googleapis.com/v1/accounts/${accountId}/locationsaccountId需要先调用账号列表接口获取当前授权用户名下的GMB账号列表,拿到带accounts/前缀的完整ID后再传入,不可直接使用普通用户ID。 - 补充必要请求参数:新接口要求必须传递
readMask查询参数指定返回字段,可根据需求填写需要返回的定位属性,示例:?readMask=name,title,address,phoneNumbers,未传递该参数会直接返回请求无效错误。 - 检查Google Cloud项目配置:确认你使用的
webClientId对应的Google Cloud项目,已启用Google My Business Business Information API,未启用该API也会触发同类访问错误。 - 修复登录逻辑代码异常:你的登录函数存在拼写错误,且
.then回调内的await未声明async,修改后参考代码:const googleSignIn = async () => { try { await GoogleSignin.hasPlayServices(); const userInfo = await GoogleSignin.signIn() if (userInfo) { const data = await GoogleSignin.getTokens(); console.log('access-token:', data.accessToken); } } catch (e) { console.log(e) } } - 原有权限配置无需调整,
https://www.googleapis.com/auth/business.manage仍适配新版本API的权限要求。
修改后的接口请求参考代码:
const getLocation = async (accountId, accessToken) => { if (accessToken) { try { const res = await fetch(`https://mybusinessbusinessinformation.googleapis.com/v1/accounts/${accountId}/locations?readMask=name,title,address`, { headers: { 'Accept': 'application/json', Authorization: `Bearer ${accessToken}`, } }) return await res.json() } catch (e) { return Promise.reject(e) } } else { Alert.alert('', 'Access token missing') } }
内容的提问来源于stack exchange,提问作者Vishal Wangoes
相关产品推荐
相关产品推荐

