flask 应用程序中的错误消息无法从 trefle API 检索

问题描述 投票:0回答:1

我正在使用 Flask 使用 trefle API 制作一个园艺网站。目标是允许用户查询植物并查找有关理想生长条件等的信息。我的 API 密钥已正确设置,但我不仅无法在 Trefle 的网站上搜索植物:https://trefle.io/ explore?search=coconut,但我也无法使用请求来获取数据。我的代码:

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)

我尝试查询植物“椰子”,但我得到:

    "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"
}

即使使用我的浏览器,我也无法检索它并得到:

We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
python api flask
1个回答
0
投票

在他们的github上搜索发现这个问题来自7月 这是 github 问题跟踪器的链接 搜索错误 #122

© www.soinside.com 2019 - 2024. All rights reserved.