微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

博览会-React Native:如何从Google Fit API读取用户数据

如何解决博览会-React Native:如何从Google Fit API读取用户数据

在此先感谢您能提供的任何帮助。我的目标是使用OAuth2从Google Fit API获取用户的步数数据。我可以使用Expo的expo-app-auth程序包进行身份验证,并接收一个accesstoken,但是,当我尝试从Fit API中读取数据时,出现错误消息,即身份验证凭据丢失。

相关代码

let config = {
  issuer: 'https://accounts.google.com',scopes: ['openid','profile','https://www.googleapis.com/auth/fitness.activity.read'],/* This is the CLIENT_ID generated from a Firebase project */
  clientId: 'xxx.apps.googleusercontent.com',};

let authState = await AppAuth.authAsync(config); // this returns an idToken,accesstoken,list of scopes configured,including the fitness.activity.read

const url = 'https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate';
const today = new Date().getTime();
const past = today - (5 * 24 * 60 * 60 * 100);
const body = {
  "aggregateBy": [{
    "dataTypeName": "com.google.step_count.delta","dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
  }],"bucketByTime": { "durationMillis": 86400000 },"startTimeMillis": today,"endTimeMillis": past
}

const header = {
  'Content-Type': 'application/json','Authorization': `BEARER ${authState.accesstoken}`
}

const req = await fetch(url,{
  method: 'POST',headers: header,body: JSON.stringify(body)
});

const resp = await req.json();
/*
Object {
  "error": Object {
    "code": 401,"errors": Array [
      Object {
        "domain": "global","location": "Authorization","locationType": "header","message": "Login required.","reason": "required",},],"message": "Request is missing required authentication credential. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","status": "UNAUTHENTICATED",}
*/


我能够使用真实设备上的内置APK来产生此问题。对于独立版本,我将Google Console中的OAuth2 clientId用作配置中的clientId,并且启用了fitness API。

有人知道我可能做错了吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。