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