]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/oauth/exchangetokens.php
Merge branch 'testing' of gitorious.org:statusnet/mainline
[quix0rs-gnu-social.git] / tests / oauth / exchangetokens.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
22
23 require_once INSTALLDIR . '/extlib/OAuth.php';
24
25 $ini = parse_ini_file("oauth.ini");
26
27 $test_consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
28
29 $at_endpoint = $ini['apiroot'] . $ini['access_token_url'];
30
31 $shortoptions = 't:s:';
32 $longoptions = array('oauth_token=', 'token_secret=');
33
34 $helptext = <<<END_OF_ETOKENS_HELP
35   exchangetokens.php [options]
36   Exchange an authorized OAuth request token for an access token
37
38     -t --oauth_token       authorized request token
39     -s --token_secret      authorized request token secret
40
41 END_OF_ETOKENS_HELP;
42
43 require_once INSTALLDIR . '/scripts/commandline.inc';
44
45 $token        = null;
46 $token_secret = null;
47
48 if (have_option('t', 'oauth_token')) {
49     $token = get_option_value('oauth_token');
50 }
51
52 if (have_option('s', 'token_secret')) {
53     $token_secret = get_option_value('s', 'token_secret');
54 }
55
56 if (empty($token)) {
57     print "Please specify a request token.\n";
58     exit(1);
59 }
60
61 if (empty($token_secret)) {
62     print "Please specify a request token secret.\n";
63     exit(1);
64 }
65
66 $rt = new OAuthToken($token, $token_secret);
67 common_debug("Exchange request token = " . var_export($rt, true));
68
69 $parsed = parse_url($at_endpoint);
70 $params = array();
71 parse_str($parsed['query'], $params);
72
73 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
74
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);
77
78 $r = httpRequest($req_req->to_url());
79
80 common_debug("Exchange request token = " . var_export($rt, true));
81 common_debug("Exchange tokens URL: " . $req_req->to_url());
82
83 $body = $r->getBody();
84
85 $token_stuff = array();
86 parse_str($body, $token_stuff);
87
88 print 'Access token        : ' . $token_stuff['oauth_token'] . "\n";
89 print 'Access token secret : ' . $token_stuff['oauth_token_secret'] . "\n";
90
91 function httpRequest($url)
92 {
93     $request = HTTPClient::start();
94
95     $request->setConfig(array(
96                               'follow_redirects' => true,
97                               'connect_timeout' => 120,
98                               'timeout' => 120,
99                               'ssl_verify_peer' => false,
100                               'ssl_verify_host' => false
101                               ));
102
103     return $request->get($url);
104 }
105