How to Get Facebook Video id From url Using Kotlin?

Hello Friends Today, through this tutorial, I will tell you How to Get Facebook Video id From url Using Kotlin Script Code?

Due to Facebook’s terms of service and potential policy changes, directly scraping information from Facebook using Kotlin is ‘not recommended’. Instead, consider using the following alternative approaches:

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’ll 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 Kotlin example using the Graph API (replace `YOUR_ACCESS_TOKEN` with your actual token).

import com.google.gson.JsonObject
import okhttp3.*

fun getFacebookVideoId(url: String): String? {
val videoIdRegex = Regex("""v=(\d+)""")
val matchResult = videoIdRegex.find(url) ?: return null

val videoId = matchResult.groups[1]?.value

val client = OkHttpClient()
val request = Request.Builder()
.url("https://graph.facebook.com/v15.0/$videoId?fields=id&access_token=YOUR_ACCESS_TOKEN")
.build()
try {
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
return null
}

val responseBody = response.body?.string() ?: return null
val jsonObject = JsonObject.parse(responseBody)
return jsonObject.get("id")?.asString
} catch (e: Exception) {
e.printStackTrace()
return null
}
}

fun main() {
val videoUrl = "https://www.facebook.com/watch/?v=1234567890"
val videoId = getFacebookVideoId(videoUrl)
if (videoId != null) {
println("Video ID: $videoId")
} else {
println("Failed to get video ID.")
}
}

Explanation.

1. The code uses regular expressions to extract the potential video ID from the URL.
2. It then builds an HTTP request to the Facebook Graph API using the extracted ID and your access token.
3. It parses the JSON response to retrieve the actual video ID if the request is successful.
4. Remember to replace `YOUR_ACCESS_TOKEN` with your actual token obtained through Facebook developer tools.

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

– Similar to Java, some third-party 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.

Seconds Methods

fun getFacebookVideoId(url: String): String? {

// Define the regular expression pattern to match Facebook video URLs
val pattern = """(?:https?://)?(?:www\.)?(?:facebook\.com/.*/videos/|facebook\.com/video\.php\?v=)(\d+)(?:\S+)?"""
try {

// Create a Regex object
val regex = Regex(pattern)

// Find the first match in the input URL
val matchResult = regex.find(url)

// If a match is found, return the video ID; otherwise, return null
return matchResult?.groupValues?.get(1)

} catch (e: Exception) {
println("Error creating regular expression: $e")
return null
}

}

fun main() {
val facebookVideoUrl = "https://www.facebook.com/facebook/videos/123456789/"
val videoId = getFacebookVideoId(facebookVideoUrl)
if (videoId != null) {

println("Facebook Video ID: $videoId")
} else {
println("Invalid Facebook Video URL")
}
}