]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - tests/oauth/getrequesttoken.php
Change temp credential test script to use POST instead of GET (more
[quix0rs-gnu-social.git] / tests / oauth / getrequesttoken.php
index fc546a0f4ce784dd8d663d73e48b907f68efa7f7..73f502af307f57cd2aed957c2431289d16bfc155 100755 (executable)
@@ -2,7 +2,7 @@
 <?php
 /*
  * StatusNet - a distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, StatusNet, Inc.
+ * Copyright (C) 2010, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -25,47 +25,74 @@ require_once INSTALLDIR . '/extlib/OAuth.php';
 
 $ini = parse_ini_file("oauth.ini");
 
-$test_consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
-
-$rt_endpoint = $ini['apiroot'] . $ini['request_token_url'];
+// Check to make sure we have everything we need from the ini file
+foreach(array('consumer_key', 'consumer_secret', 'apiroot', 'request_token_url') as $inikey) {
+    if (empty($ini[$inikey])) {
+        print "You forgot to specify a $inikey in your oauth.ini file.\n";
+        exit(1);
+    }
+}
 
-$parsed = parse_url($rt_endpoint);
-$params = array();
+$testConsumer    = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
+$requestTokenUrl = $ini['apiroot'] . $ini['request_token_url'];
+$parsed          = parse_url($requestTokenUrl);
+$params          = array();
 
 parse_str($parsed['query'], $params);
+$params['oauth_callback'] = 'oob'; // out-of-band
 
 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
 
-$req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $rt_endpoint, $params);
-$req_req->sign_request($hmac_method, $test_consumer, NULL);
+try {
+    $req = OAuthRequest::from_consumer_and_token(
+        $testConsumer,
+        null,
+        "POST",
+        $requestTokenUrl,
+        $params
+    );
+    $req->sign_request($hmac_method, $testConsumer, NULL);
+    $r = httpRequest($req->to_url());
+} catch (Exception $e) {
+    // oh noez
+    print $e->getMessage();
+    print "OAuth Request:\n";
+    var_dump($req);
+    exit(1);
+}
 
-$r = httpRequest($req_req->to_url());
+$body       = $r->getBody();
+$tokenStuff = array();
 
-$body = $r->getBody();
+parse_str($body, $tokenStuff);
 
-$token_stuff = array();
-parse_str($body, $token_stuff);
+$tok       = $tokenStuff['oauth_token'];
+$confirmed = $tokenStuff['oauth_callback_confirmed'];
 
-$authurl = $ini['apiroot'] . $ini['authorize_url'] . '?oauth_token=' . $token_stuff['oauth_token'];
+if (empty($tokenStuff['oauth_token']) || empty($confirmed) || $confirmed != 'true') {
+    print "Error: $body\n";
+    exit(1);
+}
 
-print 'Request token        : ' . $token_stuff['oauth_token'] . "\n";
-print 'Request token secret : ' . $token_stuff['oauth_token_secret'] . "\n";
-print "Authorize URL        : $authurl\n";
+$authurl = $ini['apiroot'] . $ini['authorize_url'] . '?oauth_token=' . $tok;
 
-//var_dump($req_req);
+print "\nSuccess! ";
+print "Authorize URL:\n\n$authurl\n\n";
+print "Now paste the Authorize URL into your browser and authorize your temporary credentials.\n";
 
 function httpRequest($url)
 {
     $request = HTTPClient::start();
-    
-    $request->setConfig(array(
-                             'follow_redirects' => true,
-                             'connect_timeout' => 120,
-                             'timeout' => 120,
-                             'ssl_verify_peer' => false,
-                             'ssl_verify_host' => false
-                             ));
-    
-    return $request->get($url);
-}
 
+    $request->setConfig(
+        array(
+            'follow_redirects' => true,
+           'connect_timeout' => 120,
+           'timeout' => 120,
+           'ssl_verify_peer' => false,
+           'ssl_verify_host' => false
+        )
+    );
+
+    return $request->post($url);
+}