]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/oauth/exchangetokens.php
049c0cad07439ee82cb22f62e87b3286c0707599
[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 // 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";
31         exit(1);
32     }
33 }
34
35 $consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
36
37 $endpoint = $ini['apiroot'] . $ini['access_token_url'];
38
39 $shortoptions = 't:s:v:';
40 $longoptions = array('oauth_token=', 'oauth_token_secret=', 'oauth_verifier=');
41
42 $helptext = <<<END_OF_ETOKENS_HELP
43   exchangetokens.php [options]
44   Exchange an authorized OAuth request token for an access token
45
46     -t --oauth_token        authorized request token
47     -s --oauth_token_secret authorized request token secret
48     -v --oauth_verifier     authorized request token verifier
49
50
51 END_OF_ETOKENS_HELP;
52
53 require_once INSTALLDIR . '/scripts/commandline.inc';
54
55 $token = $secret = $verifier = null;
56
57 if (have_option('t', 'oauth_token')) {
58     $token = get_option_value('t', 'oauth_token');
59 }
60
61 if (have_option('s', 'oauth_token_secret')) {
62     $secret = get_option_value('s', 'oauth_token_secret');
63 }
64
65 if (have_option('v', 'oauth_verifier')) {
66     $verifier = get_option_value('v', 'oauth_verifier');
67 }
68
69 if (empty($token)) {
70     print "Please specify the request token (--help for help).\n";
71     exit(1);
72 }
73
74 if (empty($secret)) {
75     print "Please specify the request token secret (--help for help).\n";
76     exit(1);
77 }
78
79 if (empty($verifier)) {
80     print "Please specify the request token verifier (--help for help).\n";
81     exit(1);
82 }
83
84 $rtok   = new OAuthToken($token, $secret);
85 $parsed = parse_url($endpoint);
86 parse_str($parsed['query'], $params);
87
88 $params['oauth_verifier'] = $verifier; // 1.0a
89
90 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
91
92 try {
93
94     $oauthReq = OAuthRequest::from_consumer_and_token(
95         $consumer,
96         $rtok,
97         "POST",
98         $endpoint,
99         $params
100     );
101
102     $oauthReq->sign_request($hmac_method, $consumer, $rtok);
103
104     $httpReq    = httpRequest($endpoint, $oauthReq->to_postdata());
105     $body       = $httpReq->getBody();
106
107 } catch (Exception $e) {
108     // oh noez
109     print $e->getMessage();
110     print "\nOAuth Request:\n";
111     var_dump($oauthReq);
112     exit(1);
113 }
114
115 $tokenStuff = array();
116 parse_str($body, $tokenStuff);
117
118 if (empty($tokenStuff['oauth_token']) || empty($tokenStuff['oauth_token_secret'])) {
119     print "Error! HTTP response body: $body\n";
120     exit(1);
121 }
122
123 print "Access Token\n";
124 print '   - oauth_token        = ' . $tokenStuff['oauth_token'] . "\n";
125 print '   - oauth_token_secret = ' . $tokenStuff['oauth_token_secret'] . "\n";
126
127 function httpRequest($endpoint, $poststr)
128 {
129     $request = HTTPClient::start();
130
131     $request->setConfig(
132         array(
133             'follow_redirects' => true,
134             'connect_timeout'  => 120,
135             'timeout'          => 120,
136             'ssl_verify_peer'  => false,
137             'ssl_verify_host'  => false
138         )
139     );
140
141     parse_str($poststr, $postdata);
142     return $request->post($endpoint, null, $postdata);
143 }
144