Created
November 27, 2025 21:13
-
-
Save su8/64504bc2d6a36a60e4db9f1a02615ae0 to your computer and use it in GitHub Desktop.
yt2.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <sstream> | |
| #include <string> | |
| #include <cstdlib> | |
| #include <curl/curl.h> | |
| #include <nlohmann/json.hpp> | |
| using json = nlohmann::json; | |
| std::string readFile(const std::string &path) { | |
| std::ifstream file(path, std::ios::binary); | |
| if (!file) throw std::runtime_error("Cannot open file: " + path); | |
| std::ostringstream oss; | |
| oss << file.rdbuf(); | |
| return oss.str(); | |
| } | |
| static size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp) { | |
| ((std::string*)userp)->append((char*)contents, size * nmemb); | |
| return size * nmemb; | |
| } | |
| int main(void) { | |
| try { | |
| std::string accessToken = "YOUR_OAUTH2_ACCESS_TOKEN"; | |
| std::string videoPath = "video.mp4"; | |
| std::string title = "Title name goes here"; | |
| std::string description = "Describe me"; | |
| std::string categoryId = "20"; | |
| std::string privacy = "private"; | |
| json metadata = { | |
| {"snippet", { | |
| {"title", title}, | |
| {"description", description}, | |
| {"categoryId", categoryId} | |
| }}, | |
| {"status", { | |
| {"privacyStatus", privacy} | |
| }} | |
| }; | |
| std::string metadataStr = metadata.dump(); | |
| std::string videoData = readFile(videoPath); | |
| std::string boundary = "----YouTubeBoundary123456"; | |
| std::ostringstream body; | |
| body << "--" << boundary << "\r\n" | |
| << "Content-Type: application/json; charset=UTF-8\r\n\r\n" | |
| << metadataStr << "\r\n" | |
| << "--" << boundary << "\r\n" | |
| << "Content-Type: video/mp4\r\n\r\n" | |
| << videoData << "\r\n" | |
| << "--" << boundary << "--\r\n"; | |
| std::string requestBody = body.str(); | |
| CURL *curl = curl_easy_init(); | |
| if (!curl) throw std::runtime_error("Failed to init CURL"); | |
| std::string response; | |
| struct curl_slist *headers = nullptr; | |
| headers = curl_slist_append(headers, ("Authorization: Bearer " + accessToken).c_str()); | |
| headers = curl_slist_append(headers, ("Content-Type: multipart/related; boundary=" + boundary).c_str()); | |
| curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=multipart&part=snippet,status"); | |
| curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | |
| curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody.c_str()); | |
| curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.size()); | |
| curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); | |
| curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); | |
| CURLcode res = curl_easy_perform(curl); | |
| if (res != CURLE_OK) { | |
| throw std::runtime_error("CURL error: " + std::string(curl_easy_strerror(res))); | |
| } | |
| std::cout << "YouTube API Response:\n" << response << "\n"; | |
| curl_slist_free_all(headers); | |
| curl_easy_cleanup(curl); | |
| } catch (const std::exception &ex) { | |
| std::cerr << "Error: " << ex.what() << "\n"; | |
| return EXIT_FAILURE; | |
| } | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment