So I’m not really sure if I’ve been staring at it too long, but I’m catching myself in a loop of logic here that’s not really resolving itself. I’m getting the error “401 Unauthorized - {“error”:“Unauthorized”,“status”:401"message”:“The ID in moderator_id must match the user ID found in the request’s OAuth token.”}.
As far as I can tell there’s a few things that go into this, mainly the broadcaster_id + moderator_id, which i get from the. Then an Authorization token from https://id.twitch.tv/oauth2/token, and a client-id from the twitch developer console application page.
I’ve tried a mix and match of various combinations but haven’t seemed to find which credentials I exactly need and seem to be missing something.
for some more clarification I’m attaching the code i have so far at the bottom. Any ideas would be appreciated. (note, code is in C#)
public IEnumerator BanOrTimeoutUserAsync(string broadcasterId, string moderatorId, string userId, bool isBan, int? duration = null, string reason = “Broke the rules”)
{
var url = $"https://api.twitch.tv/helix/moderation/bans?broadcaster_id={broadcasterId}&moderator_id={moderatorId}";
//data to send
Body body = new Body
{
data = new Data
{
user_id = userId, // Banned dude
reason = reason
}
};
string bodyJson = JsonConvert.SerializeObject(body, Formatting.Indented);
using (UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST))
{
request.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(bodyJson));
request.downloadHandler = new DownloadHandlerBuffer();
// -H equivalent
request.SetRequestHeader("Authorization", $"Bearer {accessToken}"); //currently from bot
request.SetRequestHeader("Client-ID", clientId); //currently from bot
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
print("Error: " + request.responseCode + " - " + request.error + " - " + request.downloadHandler.text);
}
else
{
// Handle the response JSON
print(request.downloadHandler.text);
}
}
}