]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/oauth/verifycreds.php
Rename OAuth token credential fetching script
[quix0rs-gnu-social.git] / tests / oauth / verifycreds.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 $shortoptions = 'o:s:';
26 $longoptions = array('oauth_token=', 'token_secret=');
27
28 $helptext = <<<END_OF_VERIFY_HELP
29   verifycreds.php [options]
30   Use an access token to verify credentials thru the api
31
32     -o --oauth_token       access token
33     -s --token_secret      access token secret
34
35 END_OF_VERIFY_HELP;
36
37 $token        = null;
38 $token_secret = null;
39
40 require_once INSTALLDIR . '/scripts/commandline.inc';
41
42 if (have_option('o', 'oauth_token')) {
43     $token = get_option_value('oauth_token');
44 }
45
46 if (have_option('s', 'token_secret')) {
47     $token_secret = get_option_value('s', 'token_secret');
48 }
49
50 if (empty($token)) {
51     print "Please specify an access token.\n";
52     exit(1);
53 }
54
55 if (empty($token_secret)) {
56     print "Please specify an access token secret.\n";
57     exit(1);
58 }
59
60 $ini = parse_ini_file("oauth.ini");
61
62 $test_consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
63
64 $endpoint = $ini['apiroot'] . '/account/verify_credentials.xml';
65
66 print "$endpoint\n";
67
68 $at = new OAuthToken($token, $token_secret);
69
70 $parsed = parse_url($endpoint);
71 $params = array();
72 parse_str($parsed['query'], $params);
73
74 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
75
76 $req_req = OAuthRequest::from_consumer_and_token($test_consumer, $at, "GET", $endpoint, $params);
77 $req_req->sign_request($hmac_method, $test_consumer, $at);
78
79 $r = httpRequest($req_req->to_url());
80
81 $body = $r->getBody();
82
83 print "$body\n";
84
85 //print $req_req->to_url() . "\n\n";
86
87 function httpRequest($url)
88 {
89     $request = HTTPClient::start();
90
91     $request->setConfig(array(
92                               'follow_redirects' => true,
93                               'connect_timeout' => 120,
94                               'timeout' => 120,
95                               'ssl_verify_peer' => false,
96                               'ssl_verify_host' => false
97                               ));
98
99     return $request->get($url);
100 }
101