fetch_token_creds.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. $ini = parse_ini_file("oauth.ini");
  23. // Check to make sure we have everything we need from the ini file
  24. foreach(array('consumer_key', 'consumer_secret', 'apiroot', 'access_token_url') as $inikey) {
  25. if (empty($ini[$inikey])) {
  26. print "You forgot to specify a $inikey in your oauth.ini file.\n";
  27. exit(1);
  28. }
  29. }
  30. $consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
  31. $endpoint = $ini['apiroot'] . $ini['access_token_url'];
  32. $shortoptions = 't:s:v:';
  33. $longoptions = array('oauth_token=', 'oauth_token_secret=', 'oauth_verifier=');
  34. $helptext = <<<END_OF_ETOKENS_HELP
  35. fetch_token_creds.php [options]
  36. Exchange authorized OAuth temporary credentials for token credentials
  37. (an authorized request token for an access token)
  38. -t --oauth_token authorized request token
  39. -s --oauth_token_secret authorized request token secret
  40. -v --oauth_verifier authorized request token verifier
  41. END_OF_ETOKENS_HELP;
  42. require_once INSTALLDIR . '/scripts/commandline.inc';
  43. $token = $secret = $verifier = null;
  44. if (have_option('t', 'oauth_token')) {
  45. $token = get_option_value('t', 'oauth_token');
  46. }
  47. if (have_option('s', 'oauth_token_secret')) {
  48. $secret = get_option_value('s', 'oauth_token_secret');
  49. }
  50. if (have_option('v', 'oauth_verifier')) {
  51. $verifier = get_option_value('v', 'oauth_verifier');
  52. }
  53. if (empty($token)) {
  54. print "Please specify the request token (--help for help).\n";
  55. exit(1);
  56. }
  57. if (empty($secret)) {
  58. print "Please specify the request token secret (--help for help).\n";
  59. exit(1);
  60. }
  61. if (empty($verifier)) {
  62. print "Please specify the request token verifier (--help for help).\n";
  63. exit(1);
  64. }
  65. $rtok = new OAuthToken($token, $secret);
  66. $parsed = parse_url($endpoint);
  67. parse_str($parsed['query'], $params);
  68. $params['oauth_verifier'] = $verifier; // 1.0a
  69. $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  70. try {
  71. $oauthReq = OAuthRequest::from_consumer_and_token(
  72. $consumer,
  73. $rtok,
  74. "POST",
  75. $endpoint,
  76. $params
  77. );
  78. $oauthReq->sign_request($hmac_method, $consumer, $rtok);
  79. $httpReq = httpRequest($endpoint, $oauthReq->to_postdata());
  80. $body = $httpReq->getBody();
  81. } catch (Exception $e) {
  82. // oh noez
  83. print $e->getMessage();
  84. print "\nOAuth Request:\n";
  85. var_dump($oauthReq);
  86. exit(1);
  87. }
  88. $tokenStuff = array();
  89. parse_str($body, $tokenStuff);
  90. if (empty($tokenStuff['oauth_token']) || empty($tokenStuff['oauth_token_secret'])) {
  91. print "Error! HTTP response body: $body\n";
  92. exit(1);
  93. }
  94. print "Access Token\n";
  95. print ' - oauth_token = ' . $tokenStuff['oauth_token'] . "\n";
  96. print ' - oauth_token_secret = ' . $tokenStuff['oauth_token_secret'] . "\n";
  97. function httpRequest($endpoint, $poststr)
  98. {
  99. $request = HTTPClient::start();
  100. $request->setConfig(
  101. array(
  102. 'follow_redirects' => true,
  103. 'connect_timeout' => 120,
  104. 'timeout' => 120,
  105. 'ssl_verify_peer' => false,
  106. 'ssl_verify_host' => false
  107. )
  108. );
  109. parse_str($poststr, $postdata);
  110. return $request->post($endpoint, null, $postdata);
  111. }