Firebase Dynamic Link REST API返回400 Bad Request问题求助
I’ve helped a few folks migrate from Google URL Shortener to Firebase Dynamic Links, so let’s break down the most common causes of that 400 error and how to fix them:
Verify your API endpoint and key
The correct REST endpoint for creating short links ishttps://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=YOUR_GCP_API_KEY. Make sure you’re including your valid GCP API key (from your linked project) as a query parameter—omitting this or using an invalid key will immediately throw a 400. Also double-check that the endpoint URL is fully written (your snippet cuts off atfirebasedynamiclinks.g..., which might be part of the issue).Ensure your request body is properly structured
Firebase’s API expects a specific JSON structure, and missing required fields is the #1 cause of 400s. Your request must include adynamicLinkInfoobject with at least two fields:domainUriPrefix: The app domain you got from your test Android app (e.g.,https://yourapp.page.link)link: The full, valid long URL you want to shorten
Here’s what the JSON should look like:
{ "dynamicLinkInfo": { "domainUriPrefix": "https://yourapp.page.link", "link": "https://your-super-long-url.com/path?with=all&your=params" } }If you’re using
ObjectMapper, make sure your POJO classes match these field names exactly (camelCase matters—Firebase won’t recognize misspelled or incorrectly cased fields likeDomainUriPrefix).Check your API key permissions
Head over to your GCP Console, navigate to APIs & Services > Credentials, and find the API key you’re using. Confirm two things:- The Firebase Dynamic Links API is enabled (go to APIs & Services > Library to check if it’s turned on)
- There are no restrictive filters (like IP or app restrictions) that might block your server’s request. If you have restrictions, make sure your server’s IP is allowed or temporarily remove them for testing.
Validate your long URL
Ensure the URL you’re trying to shorten is valid:- No unencoded special characters (spaces,
&,?should be properly encoded—thoughObjectMapperusually handles this, it’s worth checking) - It starts with
http://orhttps://(Firebase won’t accept URLs without a scheme)
- No unencoded special characters (spaces,
Read the error response details
A 400 Bad Request almost always includes a detailed error message in the response body. Modify your code to read the response content, even when it fails. For example:HttpResponse response = httpClient.execute(httpPost); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Error Details: " + responseBody);This will tell you exactly what’s wrong—like "Missing required field 'dynamicLinkInfo.link'" or "Invalid domainUriPrefix".
Example Working Code
Here’s a complete, minimal example using Apache HttpClient and ObjectMapper that should work if all settings are correct:
import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class FDLShortener { public static void main(String[] args) throws IOException { String apiKey = "YOUR_GCP_API_KEY"; String domainPrefix = "https://yourapp.page.link"; String longUrl = "https://your-long-url-here.com/very/long/path?with=multiple&query=params"; // Build request body DynamicLinkRequest request = new DynamicLinkRequest(); DynamicLinkInfo linkInfo = new DynamicLinkInfo(); linkInfo.setDomainUriPrefix(domainPrefix); linkInfo.setLink(longUrl); request.setDynamicLinkInfo(linkInfo); ObjectMapper mapper = new ObjectMapper(); String jsonPayload = mapper.writeValueAsString(request); try (CloseableHttpClient client = HttpClients.createDefault()) { HttpPost post = new HttpPost("https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey); post.setHeader("Content-Type", "application/json"); post.setEntity(new StringEntity(jsonPayload)); HttpResponse response = client.execute(post); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Status Code: " + response.getStatusLine().getStatusCode()); System.out.println("Response: " + responseBody); } } // POJOs matching Firebase's expected structure static class DynamicLinkRequest { private DynamicLinkInfo dynamicLinkInfo; public DynamicLinkInfo getDynamicLinkInfo() { return dynamicLinkInfo; } public void setDynamicLinkInfo(DynamicLinkInfo dynamicLinkInfo) { this.dynamicLinkInfo = dynamicLinkInfo; } } static class DynamicLinkInfo { private String domainUriPrefix; private String link; public String getDomainUriPrefix() { return domainUriPrefix; } public void setDomainUriPrefix(String domainUriPrefix) { this.domainUriPrefix = domainUriPrefix; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } } }
Start with checking the error response details first—it’ll save you a lot of guesswork. Once you fix the specific issue mentioned in the response, your request should go through successfully.
内容的提问来源于stack exchange,提问作者B-Wieg

