mirror of
https://github.com/thewesker/greptweet.git
synced 2025-12-20 12:11:05 -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:
123
Oauth.php
123
Oauth.php
@@ -1,123 +0,0 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
if (empty($argv[1])) { exit(1); }
|
||||
$urlargs = $argv[1];
|
||||
|
||||
/*
|
||||
* Get the Bearer Token, this is an implementation of steps 1&2
|
||||
* from https://dev.twitter.com/docs/auth/application-only-auth
|
||||
*/
|
||||
function get_bearer_token(){
|
||||
require("secret.php");
|
||||
// Step 1
|
||||
// step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738
|
||||
$encoded_consumer_key = urlencode($consumer_key);
|
||||
$encoded_consumer_secret = urlencode($consumer_secret);
|
||||
// step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secret
|
||||
$bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret;
|
||||
// step 1.3 - base64-encode bearer token
|
||||
$base64_encoded_bearer_token = base64_encode($bearer_token);
|
||||
// step 2
|
||||
$url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication
|
||||
$headers = array(
|
||||
"POST /oauth2/token HTTP/1.1",
|
||||
"Host: api.twitter.com",
|
||||
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1",
|
||||
"Authorization: Basic ".$base64_encoded_bearer_token."",
|
||||
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
|
||||
"Content-Length: 29"
|
||||
);
|
||||
|
||||
$ch = curl_init(); // setup a curl
|
||||
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
|
||||
curl_setopt($ch, CURLOPT_POST, 1); // send as post
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent
|
||||
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers
|
||||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$retrievedhtml = curl_exec ($ch); // execute the curl
|
||||
curl_close($ch); // close the curl
|
||||
$output = explode("\n", $retrievedhtml);
|
||||
$bearer_token = '';
|
||||
foreach($output as $line)
|
||||
{
|
||||
if($line === false)
|
||||
{
|
||||
// there was no bearer token
|
||||
}else{
|
||||
$bearer_token = $line;
|
||||
}
|
||||
}
|
||||
$bearer_token = json_decode($bearer_token);
|
||||
return $bearer_token->{'access_token'};
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates the Bearer Token
|
||||
* Should the bearer token become compromised or need to be invalidated for any reason,
|
||||
* call this method/function.
|
||||
*/
|
||||
function invalidate_bearer_token($bearer_token){
|
||||
$encoded_consumer_key = urlencode(CONSUMER_KEY);
|
||||
$encoded_consumer_secret = urlencode(CONSUMER_SECRET);
|
||||
$consumer_token = $encoded_consumer_key.':'.$encoded_consumer_secret;
|
||||
$base64_encoded_consumer_token = base64_encode($consumer_token);
|
||||
// step 2
|
||||
$url = "https://api.twitter.com/oauth2/invalidate_token"; // url to send data to for authentication
|
||||
$headers = array(
|
||||
"POST /oauth2/invalidate_token HTTP/1.1",
|
||||
"Host: api.twitter.com",
|
||||
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1",
|
||||
"Authorization: Basic ".$base64_encoded_consumer_token."",
|
||||
"Accept: */*",
|
||||
"Content-Type: application/x-www-form-urlencoded",
|
||||
"Content-Length: ".(strlen($bearer_token)+13).""
|
||||
);
|
||||
|
||||
$ch = curl_init(); // setup a curl
|
||||
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
|
||||
curl_setopt($ch, CURLOPT_POST, 1); // send as post
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$bearer_token.""); // post body/fields to be sent
|
||||
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers
|
||||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$retrievedhtml = curl_exec ($ch); // execute the curl
|
||||
curl_close($ch); // close the curl
|
||||
return $retrievedhtml;
|
||||
}
|
||||
|
||||
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",
|
||||
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1",
|
||||
"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;
|
||||
|
||||
}
|
||||
|
||||
// $bearer_token = get_bearer_token(); // bearer token seems to last
|
||||
require("secret.php");
|
||||
echo "bearer token: " . $bearer_token . "\n";
|
||||
print fetch($bearer_token, $urlargs); // search for the work 'test'
|
||||
//invalidate_bearer_token($bearer_token); // invalidate the token
|
||||
?>
|
||||
@@ -32,7 +32,7 @@ then
|
||||
test "$2" && since='&max_id='$(tail -n1 $1.txt | cut -d'|' -f1) # use max_id to get older tweets
|
||||
fi
|
||||
|
||||
while urlargs="screen_name=${1}&count=200&page=${page}${since}&include_rts=1&trim_user=0"; echo $urlargs; $(dirname $0)/Oauth.php $urlargs |
|
||||
while urlargs="screen_name=${1}&count=200&page=${page}${since}&include_rts=1&trim_user=0"; echo $urlargs; $(dirname $0)/oauth.php $urlargs |
|
||||
$(dirname $0)/json-to-text.php > $temp2; test $(wc -l < $temp2) -gt 0;
|
||||
do
|
||||
|
||||
|
||||
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