How to Get Facebook Video id From Url Using Go Script?

Hello Friends Today, through this tutorial, I will tell you How to Get Facebook Video id From url Using Go Script Code?. While directly scraping information from Facebook using Go is ‘not recommended’ due to their terms of service and potential policy changes, there are alternative ways to obtain video information:

1. Official Facebook Graph API.

– Facebook provides the Graph API ([https://developers.facebook.com/tools/explorer/](https://developers.facebook.com/tools/explorer/)) to access public information, including video data.
– To use the Graph API, you need a Facebook developer account and a registered application.
– Once registered, you can obtain an access token and make API calls to retrieve video details like ID, title, description, etc.
– Refer to the Facebook Graph API documentation for detailed instructions and code samples: [https://developers.facebook.com/docs/graph-api/reference/video/](https://developers.facebook.com/docs/graph-api/reference/video/)

Here’s a basic Go example using the Graph API (replace `YOUR_ACCESS_TOKEN` with your actual token).

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

func getFacebookVideoId(url string) (string, error) {
// Use regular expression to extract potential video ID from URL
videoIdRegex := `v=(\d+)`
match := regexp.MustCompile(videoIdRegex).FindStringSubmatch(url)
if len(match) == 0 {
return "", fmt.Errorf("failed to match video ID in URL")
}

// Build the API request URL
apiUrl := fmt.Sprintf("https://graph.facebook.com/v15.0/%s?fields=id&access_token=%s", match[1], "YOUR_ACCESS_TOKEN")

// Make the GET request to the Facebook Graph API
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, apiUrl, nil)
if err != nil {
return "", fmt.Errorf("error creating request: %w", err)
}

resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error making request: %w", err)
}
defer resp.Body.Close()

// Check for successful response
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected response status: %d", resp.StatusCode)
}

// Read and parse the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %w", err)
}

// Unmarshal the JSON response
var data map[string]interface{}
if err := json.Unmarshal(body, &data); err != nil {
return "", fmt.Errorf("error unmarshalling JSON response: %w", err)
}

// Extract the video ID from the parsed data
if videoId, ok := data["id"]; ok {
return videoId.(string), nil
}
return "", fmt.Errorf("video ID not found in response")
}

func main() {
videoUrl := "https://www.facebook.com/watch/?v=1234567890"
videoId, err := getFacebookVideoId(videoUrl)

if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Video ID:", videoId)
}

Explanation.

1. The code uses regular expressions to extract the potential video ID from the URL.
2. It builds the API request URL using the extracted ID and your access token.
3. It makes a GET request to the Facebook Graph API and checks for a successful response.
4. It parses the JSON response data and extracts the video ID if found.
5. Remember to replace `YOUR_ACCESS_TOKEN` with your actual token obtained through Facebook developer tools.

2. Third-party libraries (use with caution).

– Similar to other languages, some third-party Go libraries might claim to extract video IDs from Facebook URLs. However, their functionality, reliability, and adherence to Facebook’s terms of service are not guaranteed.
– ‘Use these libraries with extreme caution’ as they might introduce security risks or violate Facebook’s policies.
– Always thoroughly research and understand the terms and conditions of any third-party library before using it.

Other Use Method With API

package main

import (
"fmt"
"regexp"
)

func getFacebookVideoID(url string) string {
// Define the regular expression pattern to match Facebook video URLs
pattern := `(?:https?:\/\/)?(?:www\.)?(?:facebook\.com\/.*\/videos\/|facebook\.com\/video\.php\?v=)(\d+)(?:\S+)?`

// Compile the regular expression
re := regexp.MustCompile(pattern)

// Find the match in the input URL
match := re.FindStringSubmatch(url)

// If a match is found, return the video ID; otherwise, return an empty string
if len(match) > 1 {
return match[1]
}
return ""
}

func main() {
// Example usage
facebookVideoURL := "https://www.facebook.com/facebook/videos/123456789/"
videoID := getFacebookVideoID(facebookVideoURL)

if videoID != "" {
fmt.Printf("Facebook Video ID: %s\n", videoID)
} else {
fmt.Println("Invalid Facebook Video URL")
}
}