Discord机器人调用Fortnite Tracker API自定义Header请求失败问题
Fix: Fortnite Tracker Store API Not Recognizing API Key in Discord Bot
Let's break down what's going wrong here and how to fix it:
The Core Issue
You're making a common mistake: you're using the API endpoint URL (r.url) directly as your image source, instead of extracting the actual store image URL from the API's response.
When you send r.url to Discord's embed, Discord's client tries to fetch that URL directly—but it doesn't include your TRN-Api-Key header. That's why you're seeing the "no API key specified" error. The r.url is just the address of the API endpoint, not the image itself.
Corrected Code
Here's how to properly fetch and use the store image from the API response:
import requests import discord # Initialize session with your API key session = requests.Session() session.headers.update({'TRN-Api-Key': 'Insert my API Key here'}) # Make the API request r = session.get('https://api.fortnitetracker.com/v1/store') # First, check if the request succeeded if r.status_code == 200: # Parse the JSON response from the API store_data = r.json() # Extract the store image URL (adjust the key if needed—check the API docs for exact field name) # From Fortnite Tracker's docs, the store image is usually under 'imageUrl' or similar image_url = store_data.get('imageUrl') # Or check nested fields if needed if image_url: embed = discord.Embed() embed.set_image(url=image_url) await client.say(embed=embed) else: # Handle case where image URL isn't found in response await client.say("Could not find store image URL.") else: # Handle API request errors (e.g., invalid API key, rate limiting) await client.say(f"API Request failed with status code: {r.status_code}")
Key Fixes Explained
- Parse the API Response: The Fortnite Tracker API returns JSON data containing the store details (including the image URL). Use
r.json()to turn that response into a Python dictionary you can work with. - Extract the Correct Image URL: Dig into the parsed JSON to find the actual image URL field (refer to the API docs if you need to confirm the exact key name—common ones are
imageUrlorbannerUrl). - Error Handling: Add checks for successful requests and missing image URLs to avoid silent failures in your bot.
Additional Troubleshooting Tips
- Double-check that your
TRN-Api-Keyis valid and hasn't expired. - If you're still getting API key errors, try sending the header directly in a
requests.getcall to rule out session issues:r = requests.get( 'https://api.fortnitetracker.com/v1/store', headers={'TRN-Api-Key': 'Insert my API Key here'} ) - Verify the API response structure by printing
store_datato the console—this will help you confirm the exact path to the image URL.
内容的提问来源于stack exchange,提问作者Connor Price
相关产品推荐
相关产品推荐

