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 $test_consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
29 $at_endpoint = $ini['apiroot'] . $ini['access_token_url'];
31 $shortoptions = 't:s:';
32 $longoptions = array('oauth_token=', 'token_secret=');
34 $helptext = <<<END_OF_ETOKENS_HELP
35 exchangetokens.php [options]
36 Exchange an authorized OAuth request token for an access token
38 -t --oauth_token authorized request token
39 -s --token_secret authorized request token secret
43 require_once INSTALLDIR . '/scripts/commandline.inc';
48 if (have_option('t', 'oauth_token')) {
49 $token = get_option_value('oauth_token');
52 if (have_option('s', 'token_secret')) {
53 $token_secret = get_option_value('s', 'token_secret');
57 print "Please specify a request token.\n";
61 if (empty($token_secret)) {
62 print "Please specify a request token secret.\n";
66 $rt = new OAuthToken($token, $token_secret);
67 common_debug("Exchange request token = " . var_export($rt, true));
69 $parsed = parse_url($at_endpoint);
71 parse_str($parsed['query'], $params);
73 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
75 $req_req = OAuthRequest::from_consumer_and_token($test_consumer, $rt, "GET", $at_endpoint, $params);
76 $req_req->sign_request($hmac_method, $test_consumer, $rt);
78 $r = httpRequest($req_req->to_url());
80 common_debug("Exchange request token = " . var_export($rt, true));
81 common_debug("Exchange tokens URL: " . $req_req->to_url());
83 $body = $r->getBody();
85 $token_stuff = array();
86 parse_str($body, $token_stuff);
88 print 'Access token : ' . $token_stuff['oauth_token'] . "\n";
89 print 'Access token secret : ' . $token_stuff['oauth_token_secret'] . "\n";
91 function httpRequest($url)
93 $request = HTTPClient::start();
95 $request->setConfig(array(
96 'follow_redirects' => true,
97 'connect_timeout' => 120,
99 'ssl_verify_peer' => false,
100 'ssl_verify_host' => false
103 return $request->get($url);