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 exchangetokens.php [options]
44 Exchange an authorized OAuth request token for an access token
46 -t --oauth_token authorized request token
47 -s --oauth_token_secret authorized request token secret
48 -v --oauth_verifier authorized request token verifier
53 require_once INSTALLDIR . '/scripts/commandline.inc';
55 $token = $secret = $verifier = null;
57 if (have_option('t', 'oauth_token')) {
58 $token = get_option_value('t', 'oauth_token');
61 if (have_option('s', 'oauth_token_secret')) {
62 $secret = get_option_value('s', 'oauth_token_secret');
65 if (have_option('v', 'oauth_verifier')) {
66 $verifier = get_option_value('v', 'oauth_verifier');
70 print "Please specify the request token (--help for help).\n";
75 print "Please specify the request token secret (--help for help).\n";
79 if (empty($verifier)) {
80 print "Please specify the request token verifier (--help for help).\n";
84 $rtok = new OAuthToken($token, $secret);
85 $parsed = parse_url($endpoint);
86 parse_str($parsed['query'], $params);
88 $params['oauth_verifier'] = $verifier; // 1.0a
90 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
94 $oauthReq = OAuthRequest::from_consumer_and_token(
102 $oauthReq->sign_request($hmac_method, $consumer, $rtok);
104 $httpReq = httpRequest($endpoint, $oauthReq->to_postdata());
105 $body = $httpReq->getBody();
107 } catch (Exception $e) {
109 print $e->getMessage();
110 print "\nOAuth Request:\n";
115 $tokenStuff = array();
116 parse_str($body, $tokenStuff);
118 if (empty($tokenStuff['oauth_token']) || empty($tokenStuff['oauth_token_secret'])) {
119 print "Error! HTTP response body: $body\n";
123 print "Access Token\n";
124 print ' - oauth_token = ' . $tokenStuff['oauth_token'] . "\n";
125 print ' - oauth_token_secret = ' . $tokenStuff['oauth_token_secret'] . "\n";
127 function httpRequest($endpoint, $poststr)
129 $request = HTTPClient::start();
133 'follow_redirects' => true,
134 'connect_timeout' => 120,
136 'ssl_verify_peer' => false,
137 'ssl_verify_host' => false
141 parse_str($poststr, $postdata);
142 return $request->post($endpoint, null, $postdata);