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

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

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 User ID ที่แสดงจากปลายทาง 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 ไม่ได้รับอนุญาต ให้เปลี่ยนเส้นทางไปยังขั้นตอนการทำงานของ Google OAuth โดยอัตโนมัติแทนที่จะแสดง ข้อความแสดงข้อผิดพลาด