集成Google FIT API:初始授权请求中多作用域的处理
Google Fit API 多作用域授权请求正确写法
直接用空格分隔作用域会导致请求失败,因为空格属于URL非法字符,必须做编码处理,以下是两种可行方案:
方案1:使用URL编码空格(%20)分隔
https://accounts.google.com/o/oauth2/auth?client_id=[你的ClientID]&redirect_uri=https://health.silkland.com&scope=https://www.googleapis.com/auth/fitness.activity.read%20https://www.googleapis.com/auth/fitness.heart_rate.read&response_type=code&access_type=offline
方案2:使用加号(+)替代空格分隔
https://accounts.google.com/o/oauth2/auth?client_id=[你的ClientID]&redirect_uri=https://health.silkland.com&scope=https://www.googleapis.com/auth/fitness.activity.read+https://www.googleapis.com/auth/fitness.heart_rate.read&response_type=code&access_type=offline
JavaScript中推荐写法
为避免手动编码出错,建议用encodeURIComponent()自动处理编码:
// 定义需要的作用域数组 const requiredScopes = [ 'https://www.googleapis.com/auth/fitness.activity.read', 'https://www.googleapis.com/auth/fitness.heart_rate.read' ]; // 拼接并编码作用域字符串 const encodedScope = encodeURIComponent(requiredScopes.join(' ')); // 构建完整授权URL const authRequestUrl = `https://accounts.google.com/o/oauth2/auth?client_id=[你的ClientID]&redirect_uri=https://health.silkland.com&scope=${encodedScope}&response_type=code&access_type=offline`;
内容的提问来源于stack exchange,提问作者Rizwan Thachanakath Siraj
相关产品推荐
相关产品推荐

