facebook-graph-apiwhatsapp

WhatsApp Business API – Fixing Media Download Issues (Voice, Images, Videos)


Problem:

When trying to download WhatsApp media (voice messages, images, videos) via the WhatsApp Business API, the response was an HTML file instead of the actual media.

Investigation

  1. Step 1: Get Media ID URL
    First, I retrieved the media URL using the media ID:

    curl -X GET "https://graph.facebook.com/v18.0/{media_id}?access_token={access_token}"
    

    This returned a valid url pointing to lookaside.fbsbx.com.

  2. Step 2: Download Media
    I tried downloading the media file using:

    curl -X GET "{media_url}" -H "Authorization: Bearer {access_token}" -o media_file
    

    Issue: Instead of an audio file, I got an HTML document.

  3. Step 3: Checking API Response

    • I ran file media_file and saw:
      HTML document, Unicode text, UTF-8 text...
      
    • Opening the file in a text editor showed a Meta error page.
    • This suggested that either authentication failed or the wrong token was used.

Solution

  • 1. Assign Business Access to the App User

    2. Generate a Long-Lived User Access Token (Not a Page Access Token!)

    Key Learning:
    WhatsApp Business Media API requires a long-lived user access token, not a page access token.


    Final Working Command

    After fixing authentication, the correct way to download WhatsApp media:

    curl -X GET "{media_url}" -H "Authorization: Bearer {long_lived_user_access_token}" -o voice_message.ogg
    

    Then, check and play the file:

    file voice_message.ogg  # Verify file type
    ffplay voice_message.ogg  # Play audio (Linux/macOS)
    

    Media successfully downloaded and working!