mirror of
https://github.com/thewesker/greptweet.git
synced 2025-12-21 20:41:06 -05:00
Seems to work, and I'm assuming the bearer_token does not expire.
Obviously $bearer_token needs to be in secret.php
This commit is contained in:
87
oauth.php
87
oauth.php
@@ -2,70 +2,35 @@
|
||||
<?php
|
||||
|
||||
if (empty($argv[1])) { exit(1); }
|
||||
|
||||
$urlargs = $argv[1];
|
||||
parse_str($urlargs, $merge_to_oauth);
|
||||
|
||||
function buildBaseString($baseURI, $method, $params) {
|
||||
$r = array();
|
||||
ksort($params);
|
||||
foreach($params as $key=>$value){
|
||||
$r[] = "$key=" . rawurlencode($value);
|
||||
}
|
||||
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
|
||||
function fetch($bearer_token, $query){
|
||||
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?";
|
||||
$headers = array(
|
||||
"GET /1.1/statuses/user_timeline.json?".$query." HTTP/1.1",
|
||||
"Host: api.twitter.com",
|
||||
"Authorization: Bearer ".$bearer_token."",
|
||||
);
|
||||
$ch = curl_init(); // setup a curl
|
||||
curl_setopt($ch, CURLOPT_URL, $url.$query); // set url to send to
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output
|
||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||
$content = curl_exec($ch);
|
||||
list($header, $json) = explode("\r\n\r\n", $content, 2);
|
||||
curl_close($ch);
|
||||
|
||||
file_put_contents('php://stderr', $header . "\n\n");
|
||||
|
||||
// No results returned, Twitter API issue
|
||||
if (strlen($json) == 2) { exit(1); };
|
||||
|
||||
return $json;
|
||||
|
||||
}
|
||||
|
||||
function buildAuthorizationHeader($oauth) {
|
||||
$r = 'Authorization: OAuth ';
|
||||
$values = array();
|
||||
foreach($oauth as $key=>$value)
|
||||
$values[] = "$key=\"" . rawurlencode($value) . "\"";
|
||||
$r .= implode(', ', $values);
|
||||
return $r;
|
||||
}
|
||||
|
||||
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
|
||||
|
||||
// Get $oauth_access_token, $oauth_access_token_secret, $consumer_key, $consumer_secret
|
||||
// $bearer_token = get_bearer_token(); // bearer token seems to last
|
||||
require("secret.php");
|
||||
|
||||
$oauth = array( 'oauth_consumer_key' => $consumer_key,
|
||||
'oauth_nonce' => time(),
|
||||
'oauth_signature_method' => 'HMAC-SHA1',
|
||||
'oauth_token' => $oauth_access_token,
|
||||
'oauth_timestamp' => time(),
|
||||
'oauth_version' => '1.0');
|
||||
|
||||
$oauth = array_merge($oauth, $merge_to_oauth);
|
||||
|
||||
$base_info = buildBaseString($url, 'GET', $oauth);
|
||||
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
|
||||
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
|
||||
$oauth['oauth_signature'] = $oauth_signature;
|
||||
|
||||
// Make Requests
|
||||
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
|
||||
|
||||
|
||||
$feed = curl_init();
|
||||
$options = array( CURLOPT_HTTPHEADER => $header,
|
||||
CURLOPT_URL => $url . '?'. $urlargs,
|
||||
CURLOPT_HEADER => true,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_SSL_VERIFYPEER => false);
|
||||
|
||||
curl_setopt_array($feed, $options);
|
||||
$content = curl_exec($feed);
|
||||
list($header, $json) = explode("\r\n\r\n", $content, 2);
|
||||
curl_close($feed);
|
||||
|
||||
file_put_contents('php://stderr', $header . "\n\n");
|
||||
|
||||
// No results returned, Twitter API issue
|
||||
if (strlen($json) == 2) { exit(1); };
|
||||
|
||||
echo $json;
|
||||
// $twitter_data = json_decode($json);
|
||||
// print_r ($twitter_data);
|
||||
|
||||
print fetch($bearer_token, $urlargs); // search for the work 'test'
|
||||
//invalidate_bearer_token($bearer_token); // invalidate the token
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user