1

提前感谢您提供的任何帮助。我的目标是使用 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 控制台的 OAuth2 clientId 作为配置中的 clientId,并且启用了 Fitness API。

有谁知道我可能做错了什么?

4

0 回答 0