You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask应用调用Trefle API无法获取植物数据的问题求助

Flask应用调用Trefle API无法获取植物数据的问题求助

我正在用Flask和Trefle API搭建一个园艺类网站,核心功能是让用户查询植物,并获取它们的理想生长条件等信息。我已经正确配置好了API密钥,但现在遇到了棘手的问题:不仅在Trefle官网的搜索功能(比如访问https://trefle.io/explore?search=coconut)无法正常使用,连代码里用requests请求API数据也总是失败。

以下是我的代码片段:

BASE_URL = 'https://trefle.io/api/v1/plants/search'

@app.route('/plant', methods=['GET', 'POST'])
@login_required
def plant():
    if request.method == 'POST':
        plant_name = request.form.get('name') 

        if not plant_name:
            return jsonify({'error': 'Plant name is required'}), 400

        try:
            # Construct the Trefle API query
            response = requests.get(
                BASE_URL,
                params={'token': TREFLE_API_KEY,'q': plant_name} 
            )
            response.raise_for_status() 

            plant_data = response.json()

            if plant_data.get('data'):
                # Extract growing information
                plant_info = [
                    {
                        "Name": plant.get('common_name', "N/A"),
                        "Scientific Name": plant.get('scientific_name', "N/A"),
                        "Light": plant.get('main_species', {}).get('growth', {}).get('light', "unknown"),
                        "Atmospheric Humidity": plant.get('main_species', {}).get('growth', {}).get('atmospheric_humidity', "unknown"),
                        "pH": f"Best between {plant.get('main_species', {}).get('growth', {}).get('ph_min', 'unknown')} and {plant.get('main_species', {}).get('growth', {}).get('ph_max', 'unknown')}",
                        "Precipitations": f"Best between {plant.get('main_species', {}).get('growth', {}).get('precipitation_min', 'unknown')} and {plant.get('main_species', {}).get('growth', {}).get('precipitation_max', 'unknown')}",
                        "Temperature": f"Best between {plant.get('main_species', {}).get('growth', {}).get('temperature_min', 'unknown')}°C and {plant.get('main_species', {}).get('growth', {}).get('temperature_max', 'unknown')}°C",
                    }
                    for plant in plant_data['data']
                ]

                return render_template('plant.html', layout=layout_template, user_name=check_name(), plants=plant_info)

            return jsonify({'message': 'No plants found for your query.'}), 404

        except requests.exceptions.RequestException as e:
            return jsonify({'error': 'Failed to fetch data from the Trefle API', 'details': str(e)}), 500

    return render_template('plant.html', layout=layout_template, user_name=check_name(), plants=None)

当我尝试查询“coconut”这个植物时,得到了如下错误响应:

{
    "details": "500 Server Error: Internal Server Error for url: https://trefle.io/api/v1/plants/search?q=coconut&token=MY_TOKEN(the token is correct)",
    "error": "Failed to fetch data from the Trefle API"
}

更奇怪的是,我直接用浏览器访问这个API链接时,也会显示:

We're sorry, but something went wrong.

If you are the application owner check the logs for more information.

我已经确认过API密钥是正确的,有没有人遇到过类似的问题?或者能帮我排查一下可能的原因?

备注:内容来源于stack exchange,提问作者gameloft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 16:18:07