I am trying to create a PHP function to enable me to ban a user but I keep getting the error msg
“error”:“Unauthorized”,“status”:401,“message”:“Client ID is missing”
As far as I can tell I am doing everything correctly and I am passing in my client id so I am confused as to why this is failing despite attempting to find working examples online and double-checking my code. I am sure its something simple that I am missing:
function banUser($user_id) {
global $client_id;
global $broadcaster_id;
$data = array('user_id:' => $user_id,'reason:' => 'no reason');
$data = json_encode($data);
$len = strlen($data);
$ch = curl_init( 'https://api.twitch.tv/helix/moderation/bans?broadcaster_id='.$broadcaster_id.'&moderator_id='.$broadcaster_id.'' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Client-Id:' => $client_id,
'Authorization: Bearer '. $_SESSION['access_token'],
'Content-length: '.$len
));
$result = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);
echo "<pre>$result</pre>";
if ($i['http_code'] == 200) {
return true;
} else {
$errormsg = "FUNCTION banUser() THREW AN EXCEPTION: ".$i['http_code']."";
displayErrorMsg($errormsg);
}
}
I am passing a valid Oauth Access Token, I do have the correct scope including moderator:manage:banned_users , I am passing in the user_id for an existing twitch account, and I am able to do everything else without any problems in my app, so I am confused as to whats happening here.
ThatFontGuy:
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Client-Id:' => $client_id,
'Authorization: Bearer '. $_SESSION['access_token'],
'Content-length: '.$len
));
should be
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Client-Id: ' . $client_id,
'Authorization: Bearer '. $_SESSION['access_token'],
'Content-length: '.$len
));
You were missing a space after the :
of client-id
and doing a =>
instead of string concat
Thanks for the almost immediate response Barry. That got me past that error, now it returns:
“error”:“Bad Request”,“status”:400,“message”:“Missing required parameter "user_id"”
And yes I went and added a space after user_id: and reason:
Does it need to be shown as "User-Id: "?
should be
$data = array('user_id' => $user_id,'reason' => 'no reason');
No :
in the keys in the associative array needed.
Okay fixed that however its still returning the same error
“error”:“Bad Request”,“status”:400,“message”:“Missing required parameter "user_id"”
if I print_r($data) I get the user_id for the account I am testing for bans, so the data should be present
So what is your current code looking like now?
Here:
function banUser($user_id) {
global $client_id;
global $broadcaster_id;
# FROM: https://dev.twitch.tv/docs/api/reference/#ban-user
$data = array('user_id ' => $user_id,'reason ' => 'no reason');
$data = json_encode($data);
$len = strlen($data);
print_r($data);
$ch = curl_init( 'https://api.twitch.tv/helix/moderation/bans?broadcaster_id='.$broadcaster_id.'&moderator_id='.$broadcaster_id.'' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Client-Id: '. $client_id,
'Authorization: Bearer '. $_SESSION['access_token'],
'Content-length: '.$len
));
$result = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);
echo "<pre>$result</pre>";
//echo "<hr>";
//print_r($i);
if ($i['http_code'] == 200) {
return true;
} else {
$errormsg = "FUNCTION banUser() THREW AN EXCEPTION: ".$i['http_code']."";
displayErrorMsg($errormsg);
}
}
Ah I see the issue, forgot that ban needs a nest
$data = array('user_id ' => $user_id,'reason ' => 'no reason');
should be
$data = array('data' => array('user_id ' => $user_id,'reason ' => 'no reason'));
Okay made that change, but still same error msg:
“error”:“Bad Request”,“status”:400,“message”:“Missing required parameter "user_id"”
we snuck a space in after after the user_id (and the reason) keys in the payload
$data = array('data' => array('user_id' => $user_id,'reason' => 'no reason'));
Oh thats on me: “Insufficient Coffee Error In Operator”
Thanks immensely, been trying to figure this out for a few days. It now looks like its all working correctly!
How do we cheer bits for Barry. I dig that guy has syntax radar and drops into these forums. Thanks dude.
system
Closed
November 2, 2023, 1:37pm
13
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.