FatSecret Nutrition API
Complete integration with FatSecret for food data lookup AND diary logging.
⚠️ Authentication Methods
This skill supports two authentication methods for different use cases:
| Method | Use Case | User Login Required | Capabilities |
|---|
| OAuth2 (client_credentials) | Read-only access | ❌ No | Food search, barcode lookup, recipes |
| OAuth1 (3-legged) |
Full access | ✅ Yes (one-time PIN) | All above + diary logging |
Which to use?
- - Just searching foods? → OAuth2 (simpler, no user login)
- Logging to user's diary? → OAuth1 (requires user authorization)
🚀 Quick Start
1. Get API Credentials
- 1. Go to https://platform.fatsecret.com
- Register an application
- Copy your Consumer Key and Consumer Secret
2. Save Credentials
CODEBLOCK0
3. Install Dependencies
CODEBLOCK1
4a. For Read-Only (OAuth2) - No user login needed
CODEBLOCK2
4b. For Diary Logging (OAuth1) - One-time user authorization
CODEBLOCK3
📋 CLI Commands
| Command | Auth Required | Description |
|---|
| INLINECODE0 | OAuth2 | Search foods |
| INLINECODE1 |
OAuth2 | Barcode lookup |
|
recipes <query> | OAuth2 | Search recipes |
|
auth | - | Run OAuth1 authentication |
|
log | OAuth1 | Add food to diary (interactive) |
|
quick <food> [qty] [meal] | OAuth1 | Quick log to diary |
🤖 Agent Integration
For OpenClaw Agents
CODEBLOCK4
Agent Helper Functions
| Function | Description |
|---|
| INLINECODE6 | Check status, returns next step |
| INLINECODE7 |
Save API credentials |
|
complete_authentication_flow(pin) | Complete OAuth1 with PIN |
|
quick_log(food, qty, meal) | Log food to diary |
|
log_food(food_id, serving_id, grams_or_ml, meal, name) | Precise logging |
|
search_food(query, tokens) | Search foods |
⚠️ IMPORTANT: How grams_or_ml Works
The grams_or_ml parameter (called number_of_units in FatSecret API) is the ACTUAL amount, not a multiplier!
CODEBLOCK5
Examples:
| What you want | Serving type | gramsorml value |
|---|
| 156g of cookies | "100g" serving | INLINECODE15 |
| 200ml of milk |
"100ml" serving |
200 |
| 3 eggs | "1 large egg" serving |
3 |
| 2 slices of bread | "1 slice" serving |
2 |
🔐 Credential Storage
All credentials and tokens are stored locally:
| File | Contents | Created By |
|---|
| INLINECODE19 | Consumer Key/Secret | User (manual) |
| INLINECODE20 |
OAuth1 access tokens |
auth command |
|
$CONFIG_DIR/token.json | OAuth2 token (auto-refreshed) | OAuth2 client |
Where $CONFIG_DIR is ~/.config/fatsecret by default, or the value of FATSECRET_CONFIG_DIR if set.
To revoke access: Delete the config folder and revoke app access from your FatSecret account settings.
🐳 Container/Docker Environments
In containerized environments (Docker, OpenClaw sandbox), ~/.config/ may not persist across restarts. Use FATSECRET_CONFIG_DIR to point to a persistent volume:
CODEBLOCK6
OpenClaw example - add to your shell init or AGENTS.md:
CODEBLOCK7
🌐 Proxy Configuration (Optional)
Some FatSecret API plans require IP whitelisting. If needed, set a proxy:
CODEBLOCK8
If you don't need a proxy: The skill works without it. Proxy is only required if FatSecret blocks your IP.
🌍 Open Food Facts (Alternative)
For European products, use the free Open Food Facts API (no authentication):
CODEBLOCK9
📁 File Structure
CODEBLOCK10
⚠️ Security Notes
- 1. Credentials are stored locally in INLINECODE28
- OAuth1 tokens don't expire unless you revoke them
- OAuth1 grants full access to your FatSecret diary (read + write)
- To uninstall safely: Delete
~/.config/fatsecret/ and revoke app from FatSecret account
🔗 References
- - FatSecret API: https://platform.fatsecret.com/docs
- OAuth1 Guide: https://platform.fatsecret.com/docs/guides/authentication/oauth1/three-legged
- Open Food Facts: https://wiki.openfoodfacts.org/API
Changelog
v1.0.1 (2026-02-20)
- - Fixed OAuth2 client - now uses OAuth1 for all operations (food search + diary)
- Unified authentication: single OAuth1 flow works for both read and write operations
- Removed broken OAuth2 implementation
FatSecret 营养数据 API
与 FatSecret 完全集成,支持食物数据查询和饮食日记记录。
⚠️ 认证方式
本技能支持两种认证方式,适用于不同使用场景:
| 方式 | 使用场景 | 需要用户登录 | 功能 |
|---|
| OAuth2 (客户端凭证) | 只读访问 | ❌ 否 | 食物搜索、条形码查询、食谱 |
| OAuth1 (三方授权) |
完整访问 | ✅ 是(一次性 PIN 码) | 以上所有功能 + 饮食日记记录 |
如何选择?
- - 仅搜索食物? → 使用 OAuth2(更简单,无需用户登录)
- 需要记录到用户饮食日记? → 使用 OAuth1(需要用户授权)
🚀 快速开始
1. 获取 API 凭证
- 1. 访问 https://platform.fatsecret.com
- 注册一个应用
- 复制你的 Consumer Key 和 Consumer Secret
2. 保存凭证
bash
mkdir -p ~/.config/fatsecret
cat > ~/.config/fatsecret/config.json << EOF
{
consumer_key: 你的消费者密钥,
consumer_secret: 你的消费者密钥密码
}
EOF
3. 安装依赖
bash
cd /path/to/fatsecret-skill
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
4a. 只读模式(OAuth2)- 无需用户登录
bash
立即可以搜索
./scripts/fatsecret-cli.sh search 鸡胸肉
4b. 饮食日记记录(OAuth1)- 一次性用户授权
bash
运行认证流程
./scripts/fatsecret-cli.sh auth
按照提示操作:
1. 访问授权 URL
2. 使用 FatSecret 账号登录
3. 授权应用
4. 输入显示的 PIN 码
现在可以记录食物了
./scripts/fatsecret-cli.sh quick 鸡蛋 3 早餐
📋 CLI 命令
| 命令 | 需要认证 | 描述 |
|---|
| search <查询词> | OAuth2 | 搜索食物 |
| barcode <条码> |
OAuth2 | 条形码查询 |
| recipes <查询词> | OAuth2 | 搜索食谱 |
| auth | - | 运行 OAuth1 认证 |
| log | OAuth1 | 添加食物到饮食日记(交互式) |
| quick <食物> [数量] [餐次] | OAuth1 | 快速记录到饮食日记 |
🤖 智能体集成
适用于 OpenClaw 智能体
python
from scripts.fatsecretagenthelper import (
getauthenticationflow,
completeauthenticationflow,
saveusercredentials
)
检查认证状态
state = get
authenticationflow()
if state[status] == need_credentials:
# 向用户请求 Consumer Key/Secret
# 使用 saveusercredentials(key, secret) 保存
pass
elif state[status] == need_authorization:
# 向用户显示授权 URL
url = state[authorization_url]
# 用户访问 URL,授权,获取 PIN 码
# 使用 completeauthenticationflow(pin) 完成
pass
elif state[status] == already_authenticated:
# 准备使用饮食日记功能
from scripts.fatsecretdiarysimple import quick_log
quick_log(鸡蛋, quantity=3, meal=早餐)
智能体辅助函数
| 函数 | 描述 |
|---|
| getauthenticationflow() | 检查状态,返回下一步操作 |
| saveusercredentials(key, secret) |
保存 API 凭证 |
| complete
authenticationflow(pin) | 使用 PIN 码完成 OAuth1 认证 |
| quick_log(food, qty, meal) | 记录食物到饮食日记 |
| log
food(foodid, serving
id, gramsor_ml, meal, name) | 精确记录 |
| search_food(query, tokens) | 搜索食物 |
⚠️ 重要提示:gramsorml 的使用方法
gramsorml 参数(在 FatSecret API 中称为 numberofunits)是实际数量,而不是倍数!
python
❌ 错误 - 这只会记录 1.56 克(7 千卡)!
log
food(foodid, serving
100gid, 1.56, 早餐, 饼干)
✅ 正确 - 这会记录 156 克(741 千卡)
log
food(foodid, serving
100gid, 156, 早餐, 饼干)
示例:
| 你想要的数量 | 份量类型 | gramsorml 值 |
|---|
| 156 克饼干 | 100克 份量 | 156 |
| 200 毫升牛奶 |
100毫升 份量 | 200 |
| 3 个鸡蛋 | 1个大鸡蛋 份量 | 3 |
| 2 片面包 | 1片 份量 | 2 |
🔐 凭证存储
所有凭证和令牌都存储在本地:
| 文件 | 内容 | 创建方式 |
|---|
| $CONFIGDIR/config.json | Consumer Key/Secret | 用户(手动) |
| $CONFIGDIR/oauth1accesstokens.json |
OAuth1 访问令牌 | auth 命令 |
| $CONFIG_DIR/token.json | OAuth2 令牌(自动刷新) | OAuth2 客户端 |
其中 $CONFIGDIR 默认为 ~/.config/fatsecret,如果设置了 FATSECRETCONFIG_DIR 环境变量则使用该值。
要撤销访问权限: 删除配置文件夹,并在你的 FatSecret 账号设置中撤销应用访问权限。
🐳 容器/Docker 环境
在容器化环境(Docker、OpenClaw 沙箱)中,~/.config/ 可能在重启后不会持久保存。使用 FATSECRETCONFIGDIR 指向持久化卷:
bash
将环境变量设置为持久化目录
export FATSECRET
CONFIGDIR=/home/node/clawd/config/fatsecret
或者在命令前添加前缀
FATSECRET
CONFIGDIR=/persistent/path ./scripts/fatsecret-cli.sh auth
OpenClaw 示例 - 添加到你的 shell 初始化文件或 AGENTS.md:
bash
export FATSECRETCONFIGDIR=/home/node/clawd/config/fatsecret
🌐 代理配置(可选)
某些 FatSecret API 套餐需要 IP 白名单。如果需要,设置代理:
bash
环境变量
export FATSECRET_PROXY=socks5://127.0.0.1:1080
或者在 config.json 中
{
consumer_key: ...,
consumer_secret: ...,
proxy: socks5://127.0.0.1:1080
}
如果不需要代理: 本技能无需代理即可工作。仅当 FatSecret 阻止你的 IP 时才需要代理。
🌍 Open Food Facts(替代方案)
对于欧洲产品,可以使用免费的 Open Food Facts API(无需认证):
python
from scripts.openfoodfacts_client import OpenFoodFactsClient
off = OpenFoodFactsClient(country=it)
products = off.search(barilla)
product = off.get_product(8076800105735) # 条形码
📁 文件结构
fatsecret/
├── SKILL.md # 本文档
├── README.md # GitHub/ClawHub 自述文件
├── requirements.txt # Python:requests、requests[socks]
├── scripts/
│ ├── fatsecret-cli.sh # 主 CLI(bash 包装器)
│ ├── fatsecret_auth.py # OAuth1 三方认证
│ ├── fatsecretagenthelper.py # 智能体辅助函数
│ ├── fatsecretdiarysimple.py # 饮食日记记录(OAuth1)
│ ├── fatsecret_client.py # OAuth2 客户端(只读)
│ └── openfoodfacts_client.py # Open Food Facts 客户端
└── examples/
└── agentusageexample.py # 智能体集成示例
⚠️ 安全说明
- 1. 凭证存储在本地 ~/.config/fatsecret/ 目录中