oauth_verify_creds.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
  21. require_once INSTALLDIR . '/extlib/OAuth.php';
  22. $shortoptions = 't:s:';
  23. $longoptions = array('oauth_token=', 'oauth_token_secret=');
  24. $helptext = <<<END_OF_VERIFY_HELP
  25. oauth_verify_creds.php [options]
  26. Access /api/account/verify_credentials.xml with OAuth
  27. -t --oauth_token access token
  28. -s --oauth_token_secret access token secret
  29. END_OF_VERIFY_HELP;
  30. $token = null;
  31. $token_secret = null;
  32. require_once INSTALLDIR . '/scripts/commandline.inc';
  33. if (have_option('t', 'oauth_token')) {
  34. $token = get_option_value('t', 'oauth_token');
  35. }
  36. if (have_option('s', 'oauth_token_secret')) {
  37. $token_secret = get_option_value('s', 'oauth_token_secret');
  38. }
  39. if (empty($token)) {
  40. print "Please specify an access token (--help for help).\n";
  41. exit(1);
  42. }
  43. if (empty($token_secret)) {
  44. print "Please specify an access token secret (--help for help).\n";
  45. exit(1);
  46. }
  47. $ini = parse_ini_file("oauth.ini");
  48. $consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
  49. $endpoint = $ini['apiroot'] . '/account/verify_credentials.xml';
  50. $atok = new OAuthToken($token, $token_secret);
  51. $parsed = parse_url($endpoint);
  52. parse_str($parsed['query'], $params);
  53. try {
  54. $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  55. $oauthReq = OAuthRequest::from_consumer_and_token(
  56. $consumer,
  57. $atok,
  58. "GET",
  59. $endpoint,
  60. $params
  61. );
  62. $oauthReq->sign_request($hmac_method, $consumer, $atok);
  63. $httpReq = httpRequest($oauthReq->to_url());
  64. } catch (Exception $e) {
  65. print "Error! HTTP response body: " . $httpReq->getBody();
  66. exit(1);
  67. }
  68. print $httpReq->getBody();
  69. function httpRequest($url)
  70. {
  71. $request = HTTPClient::start();
  72. $request->setConfig(
  73. array(
  74. 'follow_redirects' => true,
  75. 'connect_timeout' => 120,
  76. 'timeout' => 120,
  77. 'ssl_verify_peer' => false,
  78. 'ssl_verify_host' => false
  79. )
  80. );
  81. return $request->get($url);
  82. }