Following is my code but I am not aware what I need to pass for user context auth only.
@abraham Will you please help me to short out the auth issue I am facing with my code.
Each time I am getting error like following
Error: call to token URL https://api.twitter.com/1.1/account_activity/webhooks/546329593564654/subscriptions.json failed with status 400, response {“errors”:[{“code”:215,“message”:“Bad Authentication data.”}]}, curl_error , curl_errno 0
$consumer_key = 'AGFwc3fA8aJSr';
$consumer_secret = 'jnUoybbXsOLwsXIHx7nFDEYxuOy5p1NyAdJ825';
$token = '89777824-VLeK34u2qJPusuMo';
$token_secret = 'kb3uEf4Ve926NVJalE4DLYO1k9cngCEul';
$webhook_id = 546329593564654;
$base_uri = 'https://api.twitter.com/1.1/account_activity/webhooks/546329593564654/subscriptions.json';
$nonce = time();
$timestamp = time();
$oauth = array(
‘oauth_consumer_key’ => $consumer_key,
‘oauth_consumer_secret’ => $consumer_secret,
‘oauth_token’ => $token,
‘oauth_token_secret’ => $token_secret,
‘oauth_nonce’ => $nonce,
‘oauth_signature_method’ => ‘HMAC-SHA1’,
‘oauth_timestamp’ => $timestamp,
‘oauth_version’ => ‘1.0’,
‘oauth_verify’ => true);
$fields = array(
‘consumer_key’ => $consumer_key,
‘consumer_secret’ => $consumer_secret,
‘token’ => $token,
‘token_secret’ => $token_secret,
‘webhook_id’ => $webhook_id
);
function buildBaseStringGet($base_uri, $params) {
$temp_array = array();
ksort($params);
foreach($params as $key=>$value){
$temp_array[] = “$key=” . rawurlencode($value);
}
return ‘GET&’ . rawurlencode($base_uri) . ‘&’ . rawurlencode(implode(’&’, $temp_array)); //return complete base string
}
function encodeParams($params) {
$temp_array = array();
ksort($params);
foreach($params as $key=>$value){
$temp_array[] = “$key=” . rawurlencode($value);
}
return implode(’&’, $temp_array);
}
function getCompositeKey($consumer_secret, $request_token){
return rawurlencode($consumer_secret) . ‘&’ . rawurlencode($request_token);
}
function buildAuthorizationHeader($oauth){
$head = 'Authorization: Basic ‘; //header prefix
$values = array();
foreach($oauth as $key=>$value){
$values[] = “$key=”" . rawurlencode($value) . “”";
}
$head .= implode(’, ', $values);
return $head;
}
$base_string = buildBaseStringGet($base_uri, $oauth); //build the base string
$composite_key = getCompositeKey($consumer_secret, null); //first request, no request token yet
$oauth_signature = base64_encode(hash_hmac(‘sha1’, $base_string, $composite_key, true)); //sign the base string
$oauth[‘oauth_signature’] = $oauth_signature; //add the signature to our oauth array
$header = array( buildAuthorizationHeader($oauth)); //create header array and add ‘Expect:’
$fields_string = encodeParams($fields);
$options = array(CURLOPT_HTTPHEADER => $header, //use our authorization and expect header
//CURLOPT_CAINFO => dirname(dirname(FILE)) . ‘/cacert.pem’,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => false, //don’t retrieve the header back from Twitter
CURLOPT_URL => $base_uri, //the URI we’re sending the request to
CURLOPT_POST => false, //this is going to be a POST - required
CURLOPT_RETURNTRANSFER => true, //return content as a string, don’t echo out directly
CURLOPT_SSL_VERIFYPEER => false
); //don’t verify SSL certificate, just do it
try {
$ch = curl_init();
curl_setopt_array($ch, $options); //set options
$response = curl_exec($ch);
$result = json_decode($response);
}
//catch exception
catch(Exception $e) {
echo 'Message: ’ .$e->getMessage();
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
die("Error: call to token URL $base_uri failed with status $status, response $response, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch));
} else {
print_r($result);
}
curl_close($ch); //hang up