Hello, FatSecret team
I’m currently developing a side project focused on nutrition, and I’d like to test it using your company’s API.
I'm currently unable to use the Image Recognition API and consistently receive a **401 Unauthorized** response.
It appears that the issue may be related to API scopes. On my side, I am requesting the following scopes when generating the access token:
`basic premier image-recognition`
However, requests to the Image Recognition v2 endpoint (`/rest/image-recognition/v2`) still fail with a 401 error, even though the token is successfully issued.
Could you please confirm:
* Whether the **image-recognition** scope needs to be explicitly enabled on your side for our client ID, and
* If there are any additional scopes, permissions, or account-level settings required to access the Image Recognition API?
For reference, the token is being passed correctly as a Bearer token in the Authorization header.
Thank you for your support, and please let us know if you need any additional details from our end.
FYI: Here's the part of the python code.
def get_token():
if _token_cache["token"] and time.time() < _token_cache["expires_at"]:
return _token_cache["token"]
try:
# ADDED 'image-recognition' to the scope list
payload = {
"grant_type": "client_credentials",
"scope": "basic premier image-recognition"
}
response = requests.post(TOKEN_URL, auth=(CLIENT_ID, CLIENT_SECRET), data=payload)
if response.status_code != 200:
print("Token Error:", response.text)
return None
data = response.json()
_token_cache["token"] = data["access_token"]
_token_cache["expires_at"] = time.time() + data["expires_in"] - 60
return _token_cache["token"]
except Exception as e:
print(f"Auth Exception: {e}")
return None
import base64
@app.route('/api/recognize', methods=['POST'])
def recognize_image():
if 'image' not in request.files:
return jsonify({"error": "No image uploaded"}), 400
file = request.files['image']
token = get_token()
if not token:
# If token failed (likely due to invalid_scope), return that error
return jsonify({"error": "Authentication failed. Check API Scopes."}), 401
try:
# 1. PROCESS IMAGE (Resize to 512x512 to fit 1MB limit and improve AI accuracy)
img = Image.open(file)
# Convert to RGB (in case of PNG with transparency)
if img.mode in ("RGBA", "P"):
img = img.convert("RGB")
# Resize maintaining aspect ratio or force 512x512
img.thumbnail((512, 512))
# Save to buffer
buffer = io.BytesIO()
img.save(buffer, format="JPEG", quality=85)
buffer.seek(0)
image_data = buffer.read()
# 2. ENCODE
base64_encoded = base64.b64encode(image_data).decode('utf-8')
# 3. SAFETY CHECK
if len(base64_encoded) > 999982:
return jsonify({"error": "Image too large even after resize."}), 413
payload = {
"image_b64": base64_encoded,
"include_food_data": True,
"region": "US",
"language": "en"
}
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
# 4. SEND
res = requests.post(RECOGNITION_V2_URL, headers=headers, json=payload)
if res.status_code != 200:
return jsonify(res.json()), res.status_code
return jsonify(res.json())
except Exception as e:
print(f"Exception: {str(e)}")
return jsonify({"error": str(e)}), 500
Best regards,