Get clips: Can't get clips by game_id but can from clip id

I get succes when i use getClipDownloadUrl.
But when i use getTopClipsInTimeSpan it claims a problem with the oauth token.

Error

Response text: {"error":"Unauthorized","status":401,"message":"Invalid OAuth token"}
terminate called after throwing an instance of 'std::runtime_error'
  what():  Failed to get clip download URLs: 'data' field is missing or null
Aborted

They use the same end point so should the scope not be the same?

API Helper methods

std::string TwitchApi::getOAuthToken(const std::string& client_id, const std::string& client_secret) {
    cpr::Response r = cpr::Post(cpr::Url{"https://id.twitch.tv/oauth2/token"},
                                cpr::Payload{{"client_id", client_id},
                                             {"client_secret", client_secret},
                                             {"grant_type", "client_credentials"}});

    auto json = nlohmann::json::parse(r.text);
    if(json.contains("access_token") && !json["access_token"].is_null()) {
        return json["access_token"];
    } else {
        throw std::runtime_error("Failed to get access token");
    }
}


std::map<std::string, std::string> TwitchApi::getTopGames(const std::string& access_token, const std::string& client_id, const int& count) {
    cpr::Response r = cpr::Get(cpr::Url{"https://api.twitch.tv/helix/games/top"},
                               cpr::Header{{"Client-ID", client_id},
                                           {"Authorization", "Bearer " + access_token}},
                               cpr::Parameters{{"first", std::to_string(count)}});

    auto json = nlohmann::json::parse(r.text);
    std::map<std::string, std::string> games;
    if(json.contains("data") && !json["data"].is_null()) {
        for(const auto& game : json["data"]) {
            if(!game["name"].is_null()) {
                games[game["name"]] = game["id"];
            }
        }
    } else {
        throw std::runtime_error("Failed to get top games");
    }

    return games;
}

std::string TwitchApi::getClipDownloadUrl(const std::string& access_token, const std::string& client_id, const std::string& clip_id) {
    cpr::Response r = cpr::Get(cpr::Url{"https://api.twitch.tv/helix/clips"},
                               cpr::Header{{"Client-ID", client_id},
                                           {"Authorization", "Bearer " + access_token}},
                               cpr::Parameters{{"id", clip_id}});

    auto json = nlohmann::json::parse(r.text);
    if(json.contains("data") && !json["data"].is_null() && !json["data"][0]["thumbnail_url"].is_null()) {
        std::string thumbnail_url = json["data"][0]["thumbnail_url"];
        std::string download_url = thumbnail_url.substr(0, thumbnail_url.find("-preview")) + ".mp4";
        return download_url;
    } else {
        throw std::runtime_error("Failed to get clip download URL");
    }
}

In Main

trust me on the id and secret pls


const int hours = 24*7;
const std::string clip_id = "ProtectiveDeadVultureAliens-r5smm8hR4m60iJmX";

TwitchApi ta;
std::string access_token = ta.getOAuthToken(client_id, client_secret);


std::string clip_download_url = ta.getClipDownloadUrl(access_token, client_id, clip_id);

std::vector<std::string> clip_urls = ta.getTopClipsInTimeSpan(client_id, client_secret,  "32399", hours);
1 Like

I passed client id instead of access_token like a dummy and don’t know how to delete posts.