4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
23 require_once INSTALLDIR . '/extlib/OAuth.php';
25 $ini = parse_ini_file("oauth.ini");
27 // Check to make sure we have everything we need from the ini file
28 foreach(array('consumer_key', 'consumer_secret', 'apiroot', 'access_token_url') as $inikey) {
29 if (empty($ini[$inikey])) {
30 print "You forgot to specify a $inikey in your oauth.ini file.\n";
35 $consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
37 $endpoint = $ini['apiroot'] . $ini['access_token_url'];
39 $shortoptions = 't:s:v:';
40 $longoptions = array('oauth_token=', 'oauth_token_secret=', 'oauth_verifier=');
42 $helptext = <<<END_OF_ETOKENS_HELP
43 fetch_token_creds.php [options]
45 Exchange authorized OAuth temporary credentials for token credentials
46 (an authorized request token for an access token)
48 -t --oauth_token authorized request token
49 -s --oauth_token_secret authorized request token secret
50 -v --oauth_verifier authorized request token verifier
55 require_once INSTALLDIR . '/scripts/commandline.inc.php';
57 $token = $secret = $verifier = null;
59 if (have_option('t', 'oauth_token')) {
60 $token = get_option_value('t', 'oauth_token');
63 if (have_option('s', 'oauth_token_secret')) {
64 $secret = get_option_value('s', 'oauth_token_secret');
67 if (have_option('v', 'oauth_verifier')) {
68 $verifier = get_option_value('v', 'oauth_verifier');
72 print "Please specify the request token (--help for help).\n";
77 print "Please specify the request token secret (--help for help).\n";
81 if (empty($verifier)) {
82 print "Please specify the request token verifier (--help for help).\n";
86 $rtok = new OAuthToken($token, $secret);
87 $parsed = parse_url($endpoint);
88 parse_str($parsed['query'], $params);
90 $params['oauth_verifier'] = $verifier; // 1.0a
92 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
96 $oauthReq = OAuthRequest::from_consumer_and_token(
104 $oauthReq->sign_request($hmac_method, $consumer, $rtok);
106 $httpReq = httpRequest($endpoint, $oauthReq->to_postdata());
107 $body = $httpReq->getBody();
109 } catch (Exception $e) {
111 print $e->getMessage();
112 print "\nOAuth Request:\n";
117 $tokenStuff = array();
118 parse_str($body, $tokenStuff);
120 if (empty($tokenStuff['oauth_token']) || empty($tokenStuff['oauth_token_secret'])) {
121 print "Error! HTTP response body: $body\n";
125 print "Access Token\n";
126 print ' - oauth_token = ' . $tokenStuff['oauth_token'] . "\n";
127 print ' - oauth_token_secret = ' . $tokenStuff['oauth_token_secret'] . "\n";
129 function httpRequest($endpoint, $poststr)
131 $request = HTTPClient::start();
135 'follow_redirects' => true,
136 'connect_timeout' => 120,
138 'ssl_verify_peer' => false,
139 'ssl_verify_host' => false
143 parse_str($poststr, $postdata);
144 return $request->post($endpoint, null, $postdata);