UI: Migrate to Helix Twitch API
This commit is contained in:
parent
442886c0aa
commit
92f5afae51
@ -22,7 +22,6 @@ using namespace json11;
|
|||||||
|
|
||||||
#define TWITCH_AUTH_URL "https://obsproject.com/app-auth/twitch?action=redirect"
|
#define TWITCH_AUTH_URL "https://obsproject.com/app-auth/twitch?action=redirect"
|
||||||
#define TWITCH_TOKEN_URL "https://obsproject.com/app-auth/twitch-token"
|
#define TWITCH_TOKEN_URL "https://obsproject.com/app-auth/twitch-token"
|
||||||
#define ACCEPT_HEADER "Accept: application/vnd.twitchtv.v5+json"
|
|
||||||
|
|
||||||
#define TWITCH_SCOPE_VERSION 1
|
#define TWITCH_SCOPE_VERSION 1
|
||||||
|
|
||||||
@ -48,26 +47,17 @@ TwitchAuth::TwitchAuth(const Def &d) : OAuthStreamKey(d)
|
|||||||
&TwitchAuth::TryLoadSecondaryUIPanes);
|
&TwitchAuth::TryLoadSecondaryUIPanes);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TwitchAuth::GetChannelInfo()
|
bool TwitchAuth::MakeApiRequest(const char *path, Json &json_out)
|
||||||
try {
|
{
|
||||||
std::string client_id = TWITCH_CLIENTID;
|
std::string client_id = TWITCH_CLIENTID;
|
||||||
deobfuscate_str(&client_id[0], TWITCH_HASH);
|
deobfuscate_str(&client_id[0], TWITCH_HASH);
|
||||||
|
|
||||||
if (!GetToken(TWITCH_TOKEN_URL, client_id, TWITCH_SCOPE_VERSION))
|
std::string url = "https://api.twitch.tv/helix/";
|
||||||
return false;
|
url += std::string(path);
|
||||||
if (token.empty())
|
|
||||||
return false;
|
|
||||||
if (!key_.empty())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
std::string auth;
|
|
||||||
auth += "Authorization: OAuth ";
|
|
||||||
auth += token;
|
|
||||||
|
|
||||||
std::vector<std::string> headers;
|
std::vector<std::string> headers;
|
||||||
headers.push_back(std::string("Client-ID: ") + client_id);
|
headers.push_back(std::string("Client-ID: ") + client_id);
|
||||||
headers.push_back(ACCEPT_HEADER);
|
headers.push_back(std::string("Authorization: Bearer ") + token);
|
||||||
headers.push_back(std::move(auth));
|
|
||||||
|
|
||||||
std::string output;
|
std::string output;
|
||||||
std::string error;
|
std::string error;
|
||||||
@ -76,8 +66,7 @@ try {
|
|||||||
bool success = false;
|
bool success = false;
|
||||||
|
|
||||||
auto func = [&]() {
|
auto func = [&]() {
|
||||||
success = GetRemoteFile("https://api.twitch.tv/kraken/channel",
|
success = GetRemoteFile(url.c_str(), output, error, &error_code,
|
||||||
output, error, &error_code,
|
|
||||||
"application/json", "", nullptr,
|
"application/json", "", nullptr,
|
||||||
headers, nullptr, 5);
|
headers, nullptr, 5);
|
||||||
};
|
};
|
||||||
@ -100,24 +89,44 @@ try {
|
|||||||
if (!success || output.empty())
|
if (!success || output.empty())
|
||||||
throw ErrorInfo("Failed to get text from remote", error);
|
throw ErrorInfo("Failed to get text from remote", error);
|
||||||
|
|
||||||
Json json = Json::parse(output, error);
|
json_out = Json::parse(output, error);
|
||||||
if (!error.empty())
|
if (!error.empty())
|
||||||
throw ErrorInfo("Failed to parse json", error);
|
throw ErrorInfo("Failed to parse json", error);
|
||||||
|
|
||||||
error = json["error"].string_value();
|
error = json_out["error"].string_value();
|
||||||
if (!error.empty()) {
|
if (!error.empty())
|
||||||
if (error == "Unauthorized") {
|
throw ErrorInfo(error, json_out["message"].string_value());
|
||||||
if (RetryLogin()) {
|
|
||||||
return GetChannelInfo();
|
|
||||||
}
|
|
||||||
throw ErrorInfo(error, json["message"].string_value());
|
|
||||||
}
|
|
||||||
throw ErrorInfo(error,
|
|
||||||
json["error_description"].string_value());
|
|
||||||
}
|
|
||||||
|
|
||||||
name = json["name"].string_value();
|
return true;
|
||||||
key_ = json["stream_key"].string_value();
|
}
|
||||||
|
|
||||||
|
bool TwitchAuth::GetChannelInfo()
|
||||||
|
try {
|
||||||
|
std::string client_id = TWITCH_CLIENTID;
|
||||||
|
deobfuscate_str(&client_id[0], TWITCH_HASH);
|
||||||
|
|
||||||
|
if (!GetToken(TWITCH_TOKEN_URL, client_id, TWITCH_SCOPE_VERSION))
|
||||||
|
return false;
|
||||||
|
if (token.empty())
|
||||||
|
return false;
|
||||||
|
if (!key_.empty())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
Json json;
|
||||||
|
bool success = MakeApiRequest("users", json);
|
||||||
|
|
||||||
|
if (!success)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
name = json["data"][0]["login"].string_value();
|
||||||
|
|
||||||
|
std::string path = "streams/key?broadcaster_id=" +
|
||||||
|
json["data"][0]["id"].string_value();
|
||||||
|
success = MakeApiRequest(path.c_str(), json);
|
||||||
|
if (!success)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
key_ = json["data"][0]["stream_key"].string_value();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (ErrorInfo info) {
|
} catch (ErrorInfo info) {
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include <json11.hpp>
|
||||||
#include "auth-oauth.hpp"
|
#include "auth-oauth.hpp"
|
||||||
|
|
||||||
class BrowserDock;
|
class BrowserDock;
|
||||||
@ -31,6 +32,7 @@ class TwitchAuth : public OAuthStreamKey {
|
|||||||
virtual void SaveInternal() override;
|
virtual void SaveInternal() override;
|
||||||
virtual bool LoadInternal() override;
|
virtual bool LoadInternal() override;
|
||||||
|
|
||||||
|
bool MakeApiRequest(const char *path, json11::Json &json_out);
|
||||||
bool GetChannelInfo();
|
bool GetChannelInfo();
|
||||||
|
|
||||||
virtual void LoadUI() override;
|
virtual void LoadUI() override;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user