Guest User

OAUTH Issue

a guest
Oct 14th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.54 KB | None | 0 0
  1. <?php
  2. /*
  3.  * test_oauth_client.php
  4.  *
  5.  * @(#) $Id: test_oauth_client.php,v 1.5 2013/07/31 11:48:04 mlemos Exp $
  6.  *
  7.  */
  8.  
  9.     /*
  10.      *  Get the http.php file from http://www.phpclasses.org/httpclient
  11.      */
  12.     require('../http-client/http.php');
  13.     require('oauth_client.php');
  14.  
  15.     /* Create the OAuth authentication client class */
  16.     $client = new oauth_client_class;
  17.  
  18.     /*
  19.      * Set to true if you want to make the class dump
  20.      * debug information to PHP error log
  21.      */
  22.     $client->debug = true;
  23.  
  24.     /*
  25.      * Set to true if you want to make the class also dump
  26.      * debug output of the HTTP requests it sends.
  27.      */
  28.     $client->debug_http = false;
  29.  
  30.     /* OAuth server type name
  31.      * Setting this variable to one of the built-in supported OAuth servers
  32.      * will make the class automatically set all parameters specific of that
  33.      * type of server.
  34.      *
  35.      * Currently, built-in supported types are: Facebook, github, Google,
  36.      * Microsoft, Foursquare, Twitter and Yahoo.
  37.      *
  38.      * Send e-mail to mlemos at acm.org if you would like the class to have
  39.      * built-in support to access other OAuth servers.
  40.      *
  41.      * Set to an empty string to use another type of OAuth server. Check the
  42.      * documentation to learn how to set other parameters to configure the
  43.      * class to access that server
  44.      */
  45.     $client->server = 'Twitter';
  46.  
  47.     /* OAuth authentication URL identifier
  48.      * This should be the current page URL without any request parameters
  49.      * used by OAuth, like state and code, error, denied, etc..
  50.      */
  51.     $client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].
  52.         dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/test_oauth_client.php';
  53.  
  54.     /* OAuth client identifier
  55.      * Set this to values defined by the OAuth server for your application
  56.      */
  57.     $client->client_id = 'SET YOURS';
  58.  
  59.     /* OAuth client secret
  60.      * Set this to values defined by the OAuth server for your application
  61.      */
  62.     $client->client_secret = 'SET YOURS';
  63.  
  64.     /* OAuth client permissions
  65.      * Set this to the name of the permissions you need to access the
  66.      * application API
  67.      */
  68.     $client->scope = '';
  69.    
  70.     /* Process the OAuth server interactions */
  71.     if(($success = $client->Initialize()))
  72.     {
  73.         /*
  74.          * Call the Process function to make the class dialog with the OAuth
  75.          * server. If you previously have retrieved the access token and set
  76.          * the respective class variables manually, you may skip this call and
  77.          * use the CallAPI function directly.
  78.          */
  79.         $success = $client->Process();
  80.         // Make sure the access token was successfully obtained before making
  81.         // API calls
  82.  
  83.     // MUST BE SET TO FALSE IF I WANT THAT API CALLS USING PARAMETERS ARRAY TO SUCCEED
  84.         // $client->authorization_header = false;
  85.  
  86.         $client->CallAPI('https://api.twitter.com/1.1/account/verify_credentials.json','GET', array(), array('FailOnAccessError'=>false,'ConvertObjects'=>1),$users);
  87.         echo "<pre>";
  88.         print_r($users);
  89.         echo "</pre>";
  90.  
  91.         $client->CallAPI('https://api.twitter.com/1.1/followers/ids.json','GET', array(), array('FailOnAccessError'=>false,'ConvertObjects'=>1),$followers);
  92.         echo "<pre>";
  93.         print_r($followers);
  94.         echo "</pre>";
  95.  
  96.         $client->CallAPI('https://api.twitter.com/1.1/followers/ids.json','GET', array(), array('FailOnAccessError'=>false,'ConvertObjects'=>1),$followers);
  97.         echo "<pre>";
  98.         print_r($followers);
  99.         echo "</pre>";
  100.  
  101.     // NOT WORKING IF authorization_header IS NOT SET TO FALSE
  102.         $client->CallAPI('https://api.twitter.com/1.1/search/tweets.json','GET', array('q' => 'factornews'), array('FailOnAccessError'=>false,'ConvertObjects'=>1),$tweets);
  103.         echo "<pre>";
  104.         print_r($tweets);
  105.         echo "</pre>";
  106.  
  107.  
  108.         /*
  109.          * if(strlen($client->access_token))
  110.          * {
  111.          *   $success = $client->CallAPI();
  112.          * }
  113.          */
  114.        
  115.         /* Internal cleanup call
  116.          */
  117.         $success = $client->Finalize($success);
  118.     }
  119.     /*
  120.      * If the exit variable is true, the script must not output anything
  121.      * else and exit immediately
  122.      */
  123.     if($client->exit)
  124.         exit;
  125.    
  126.     if($success)
  127.     {
  128.         /*
  129.          * The Output function call is here just for debugging purposes
  130.          * It is not necessary to call it in real applications
  131.          */
  132.         $client->Output();
  133.     }
  134.     else
  135.     {
  136.         /*
  137.          * If there was an unexpected error, display to the user
  138.          * some useful information
  139.          */
  140. ?>
  141. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  142. <html>
  143. <head>
  144. <title>OAuth client error</title>
  145. </head>
  146. <body>
  147. <h1>OAuth client error</h1>
  148. <pre>Error: <?php echo HtmlSpecialChars($client->error); ?></pre>
  149. </body>
  150. </html>
  151. <?php
  152.     }
  153.  
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment