ตัวอย่างการติดตั้งใช้งาน

หากต้องการย้ายข้อมูลจาก Fitbit Web API เดิมไปยัง Google Health API คุณจะต้องย้ายจากไลบรารี OAuth2 ทั่วไปไปยังไลบรารีการตรวจสอบสิทธิ์ของ Google ต่อไปนี้คือ คำแนะนำด้านสถาปัตยกรรมและการใช้งานรหัสเทียมที่เขียนด้วย JavaScript เพื่อจัดการสถานะ "Dual-Library" นี้

1. "การเปลี่ยนมิดเดิลแวร์"

เนื่องจากคุณไม่สามารถย้ายข้อมูลผู้ใช้ทั้งหมดได้ในครั้งเดียว แบ็กเอนด์จึงต้องพิจารณา ว่าจะใช้ไลบรารีใดตามapiVersionปัจจุบันของผู้ใช้ที่จัดเก็บไว้ในฐานข้อมูล

การใช้งาน

const { OAuth2Client } = require('google-auth-library');
const FitbitV1Strategy = require('fitbit-oauth2-library').Strategy;

// 1. Initialize the Google Health API Client
const GHAClient = new OAuth2Client(
  process.env.GOOGLE_CLIENT_ID,
  process.env.GOOGLE_CLIENT_SECRET,
  process.env.REDIRECT_URI
);

// 2. Create a Unified Fetcher
async function fetchSteps(user) {
  if (user.apiVersion === 4) {
    // ---- GOOGLE OAUTH LIBRARY LOGIC ----
    GHAClient.setCredentials({ refresh_token: user.refreshToken });
    const url = 'GET https://health.googleapis.com/v4/users/me/dataTypes/steps/dataPoints';
    const res = await GHAClient.request({ url });
    return res.data;
  } else {
    // ---- FITBIT WEB API LEGACY LOGIC ----
    // Use your existing Fitbit open-source library logic here
    return callLegacyV1Api(user.accessToken);
  }
}

2. ย้ายข้อมูลโฟลว์ UX

หากต้องการเพิ่มการรักษาผู้ใช้ ให้ใช้รูปแบบ "ขัดจังหวะและอัปเกรด" ซึ่งจะช่วยให้ผู้ใช้ไม่ต้องลงชื่อเข้าใช้อีกครั้งจนกว่าจะมีการใช้งานแอป

ตรรกะการเปลี่ยนเส้นทาง

เมื่อผู้ใช้ Fitbit Web API ใช้ฟีเจอร์ที่เฉพาะเจาะจง ให้ทริกเกอร์การย้ายข้อมูลโดยทำดังนี้

app.get('/dashboard', async (req, res) => {
  const user = await db.users.find(req.user.id);

  if (user.apiVersion === 1) {
    // Render a "soft" migration page explaining the Google transition
    return res.render('migrate-to-google', {
      title: "Keep your data syncing",
      message: "Fitbit is moving to Google accounts. Re-connect now to stay updated."
    });
  }

  const data = await fetchSteps(user);
  res.render('dashboard', { data });
});

3. การเปลี่ยนแปลงทางเทคนิคที่สำคัญ

เมื่อเขียนสคริปต์การย้ายข้อมูล JavaScript โปรดคำนึงถึงความแตกต่างต่อไปนี้

ฟีเจอร์ Fitbit Web API (เดิม) Google Health API (Google-Identity)
ปลายทางของโทเค็น https://api.fitbit.com/oauth2/token https://oauth2.googleapis.com/token
ไลบรารีการตรวจสอบสิทธิ์ โอเพนซอร์สมาตรฐาน Google Auth
ขอบเขต กิจกรรม https://www.googleapis.com/auth/googlehealth.activity_and_fitness
User ID รหัสที่เข้ารหัสของ Fitbit ที่แสดงในการตอบกลับ /oauth2/token รหัสผู้ใช้ที่ส่งคืนจากปลายทาง users.getIdentity

4. รายการตรวจสอบการเก็บรักษา

  • การคงอยู่ของเซสชัน: อย่าล้างเซสชัน Fitbit Web API เก่าของผู้ใช้จนกว่าจะยืนยัน access_token ของ Google Health API ได้สำเร็จและบันทึกลงในฐานข้อมูล
  • การเพิกถอนอัตโนมัติ: เมื่อการย้ายข้อมูล Google Health API เสร็จสมบูรณ์แล้ว ให้ใช้คำขอ POST ไปยังปลายทางการเพิกถอน Fitbit เดิมที่ https://api.fitbit.com/oauth2/revoke ซึ่งจะช่วยให้ผู้ใช้ไม่เห็นสิทธิ์ของแอปที่ "ซ้ำกัน" ในการตั้งค่า Fitbit
  • การจัดการข้อผิดพลาด: หากการเรียก Fitbit แสดงผลเป็น 401 Unauthorized ให้เปลี่ยนเส้นทางไปยังขั้นตอนการให้สิทธิ์ OAuth ของ Google โดยอัตโนมัติแทนที่จะแสดง ข้อความแสดงข้อผิดพลาด