Ban API Call: The ID in moderator_id must match the user ID found in the request's OAuth token

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);
            }
        }
    }

So to ban someuser from freds channel and acting as the moderator bob

You need to request permission from bob and create a user token.
So you tell bob to use your tool to login and generate an access token (with the required scope of moderator:manage:banned_users)

Assumining bob is a moderator of fred

You can now convert the three user names to ID’s and call the API

You can also do So to ban someuser from freds channel and acting as the moderator fred as the broadcaster is also a moderator of their channel, you can get the token from the broadcaster instead.

But generally this is gonna be the channel bot.

So for a practical example

  • channel cohhcarnage
  • moderator barrycarlyon
  • chatterToBan noylraCyrraB

So, using the token for barrycarlyon

  • broadcaster_id 26610234
  • moderator_id 15185913
  • user_id 61788418

So you are either

  • using a client credentials token (which should raise a “not user token” error)
  • using a token that is not a user token for the moderator you are trying to act as

Thank you. Where I mainly got caught up in was I kept using the wrong Token since I was calling the wrong API endpoint to get credentials.

Needed to use this one to get the code then the OAuth token:
//https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&scope=REQUESTED_SCOPES&state=RANDOMLY_GENERATED_STRING

Again thank you for you’re help and clearing up the issue when it seemed pretty complex at the time. I hope you have an amazing day!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.