youtube code
Here is an example of C code that uses the YouTube API to search for videos:
#include <stdio.h>
#include <curl/curl.h>
size_t write_callback(void contents, size_t size, size_t nmemb, void userp) {
size_t total_size = size * nmemb;
printf("%.s\n", total_size, (char )contents);
return total_size;
}
int main() {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
char search_query[100];
printf("Enter search query: ");
scanf("%s", search_query);
char url[200];
sprintf(url, "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%s&key=YOUR_API_KEY", search_query);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
return 0;
}
Make sure to replace "YOUR_API_KEY" in the URL with your actual YouTube API key. This code prompts the user to enter a search query, constructs the API URL with the query and the API key, and performs a GET request to retrieve the search results. The write_callback
function is used to print the response to the console.