Failed to get response - 00-514ae69a94cf3ba3ff86dd5516329932-04544c871f3192dd-00

55 views
Skip to first unread message

Abs Shakeel

unread,
Mar 19, 2025, 5:14:25 AM3/19/25
to fatsecret Platform API
An error occurred while processing your request.
Show Request ID

00-514ae69a94cf3ba3ff86dd5516329932-04544c871f3192dd-00

Our team has been notified of this issue.

code:
===
const express = require('express');
const router = express.Router();
const fetch = require('node-fetch');
const auth = require('../../middleware/auth');
const FoodImage = require('../../models/FoodImage');

router.post('/analyze', auth, async (req, res) => {
  try {
    const { imageId, imageUrl } = req.body;

    if (!imageId || !imageUrl) {
      return res.status(400).json({ msg: 'Image ID and URL are required' });
    }

    // Find image in the database
    let foodImage = await FoodImage.findById(imageId) || await FoodImage.findOne({ imageId });
    if (!foodImage) {
      return res.status(404).json({ msg: 'Image not found' });
    }

    console.log('Authenticating with FatSecret API...');
   
    // Step 1: Obtain OAuth Token
    const tokenResponse = await fetch('https://oauth.fatsecret.com/connect/token', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      body: new URLSearchParams({
        'grant_type': 'client_credentials',
        'client_id': process.env.FATSECRET_CLIENT_ID,
        'client_secret': process.env.FATSECRET_CLIENT_SECRET
      })
    });

    const tokenData = await tokenResponse.json();
    if (!tokenResponse.ok) {
      console.error('Failed to authenticate:', tokenData);
      return res.status(500).json({ error: 'Authentication failed', details: tokenData });
    }

    console.log('Authenticated successfully. Access Token received.');

    // Step 2: Download Image & Convert to Base64
    console.log('Downloading image from:', imageUrl);
    const imageResponse = await fetch(imageUrl);
    if (!imageResponse.ok) {
      return res.status(500).json({ error: `Image download failed: ${imageResponse.statusText}` });
    }
    const imageBuffer = await imageResponse.buffer();
    const base64Image = imageBuffer.toString('base64');
    console.log('Image abs converted to Base64 successfully.');

    // Step 3: Send Image to FatSecret for Analysis
    const recognitionResponse = await fetch(`https://platform.fatsecret.com/v1/image.recognition?access_token=${tokenData.access_token}`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ image_base64: base64Image })
    });

    // Debugging raw response
    const rawResponse = await recognitionResponse.text();
    console.log("Raw FatSecret Response:", rawResponse);

    if (!recognitionResponse.ok) {
      return res.status(500).json({ error: 'FatSecret API call failed', details: rawResponse });
    }

    // Step 4: Parse Response
    let recognitionData;
    try {
      recognitionData = JSON.parse(rawResponse);
    } catch (error) {
      console.error('Error parsing JSON response:', error.message);
      return res.status(500).json({ error: 'Failed to parse API response', details: rawResponse });
    }

    console.log("Parsed FatSecret Data:", recognitionData);

    // Step 5: Process Nutrition Data
    let nutritionData = {
      calories: 0,
      carbohydrates: 0,
      protein: 0,
      fat: 0,
      fiber: 0,
      sugar: 0,
      sodium: 0,
      additionalInfo: {}
    };

    if (recognitionData.food_response?.total_nutritional_content) {
      const nutrition = recognitionData.food_response.total_nutritional_content;
      nutritionData = {
        calories: parseFloat(nutrition.calories) || 0,
        carbohydrates: parseFloat(nutrition.carbohydrate) || 0,
        protein: parseFloat(nutrition.protein) || 0,
        fat: parseFloat(nutrition.fat) || 0,
        fiber: parseFloat(nutrition.fiber) || 0,
        sugar: parseFloat(nutrition.sugar) || 0,
        sodium: parseFloat(nutrition.sodium) || 0,
        additionalInfo: {
          foodName: recognitionData.food_response.food_entry_name || '',
          brandName: recognitionData.food_response.brand_name || '',
          foodId: recognitionData.food_response.food_id || ''
        }
      };
    }

    // Step 6: Save Nutrition Data in Database
    foodImage.nutritionData = nutritionData;
    await foodImage.save();

    res.json(foodImage);
  } catch (err) {
    console.error("Server Error:", err.message);
    res.status(500).json({ error: 'Server Error', details: err.message });
  }
});

module.exports = router;

seba...@fatsecret.com

unread,
Mar 19, 2025, 5:14:51 AM3/19/25
to fatsecret Platform API
Thanks for reaching out. This feature is an optional add-on and billed per usage. Please contact our API team to get a quote and trial access:
Reply all
Reply to author
Forward
0 new messages