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

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 is https://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 at firebasedynamiclinks.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 a dynamicLinkInfo object 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 like DomainUriPrefix).

  • 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:

    1. The Firebase Dynamic Links API is enabled (go to APIs & Services > Library to check if it’s turned on)
    2. 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—though ObjectMapper usually handles this, it’s worth checking)
    • It starts with http:// or https:// (Firebase won’t accept URLs without a scheme)
  • 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:27:39