PHP Classes

Very basic Oauth usage

Recommend this page to a friend!

      PHP OAuth Library  >  All threads  >  Very basic Oauth usage  >  (Un) Subscribe thread alerts  
Subject:Very basic Oauth usage
Summary:Very basic Oauth usage
Messages:4
Author:ben vandyk
Date:2013-07-17 21:24:26
Update:2013-08-03 03:10:30
 

  1. Very basic Oauth usage   Reply   Report abuse  
Picture of ben vandyk ben vandyk - 2013-07-17 21:24:27
Hi,

Sorry for the really silly question.

I have some code which works with the OAuth package, however I need to get it working with this instead as my hosting company do not include Oauth.

This is the code I need to get working:

<?php

try
{

$oauth_key = "aaa";
$oauth_secret = "bbb";


$oauth = new OAuth($oauth_key, $oauth_secret,OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);

$oauth ->fetch("https://api.redflagalert.net/1.0/company/directors/00001346.xml",'','GET');
echo $oauth->getLastResponse();
}
catch (oAuthException $e)
{
print_r($e);
}


?>

Could someone help me rewrite this using the PHP version of Oauth?

Thanks a million!
Ben

  2. Re: Very basic Oauth usage   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-07-17 21:55:15 - In reply to message 1 from ben vandyk
It seems you should set the client_id variable to $oauth_key and client_secret to $oauth_secret . The fetch call should be replaced by CallAPI. Take a look the existing example scripts to see how it works as usual.

  3. Re: Very basic Oauth usage   Reply   Report abuse  
Picture of ben vandyk ben vandyk - 2013-08-02 21:52:28 - In reply to message 2 from Manuel Lemos
Hi Manuel,

Thanks very much for your help so far. I seem to have managed to get the API to authenticate me to the server but now I am stuck. I am not sure how to get my data from the query!

When I run my script using the OAuth built in package then I get returned a page full of XML which I can process. However with your API I just get a message saying "you have logged in successfully with Redflag!"

Are you able to point me in the right direction?

Here's what I have so far (adapted from the BitBucket example):

<?php
/*
* login_with_bitbucket.php
*
* @(#) $Id: login_with_bitbucket.php,v 1.2 2013/07/31 11:48:04 mlemos Exp $
*
*/

/*
* Get the http.php file from http://www.phpclasses.org/httpclient
*/
require('http.php');
require('oauth_client.php');

$client = new oauth_client_class;
$client->debug = false;
$client->debug_http = true;
$client->server = 'Redflag';
$client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].
dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/test_oauth_client.php';

//$client->client_id = '';

$client->client_id = 'aaa';


$application_line = __LINE__;
//$client->client_secret = '';

$client->client_secret = 'bbb';

if(strlen($client->client_id) == 0
|| strlen($client->client_secret) == 0)
die('Please go to Bitbucket page to Manage Account '.
'https://bitbucket.org/account/ , click on Integrated Applications, '.
'then Add Consumer, and in the line '.$application_line.
' set the client_id with Key and client_secret with Secret. '.
'The URL must be '.$client->redirect_uri);

if(($success = $client->Initialize()))
{
if(($success = $client->Process()))
{
if(strlen($client->access_token))
{
$success = $client->CallAPI(
'https://api.redflagalert.net/1.0/company/directors/00001346.xml',
'GET', array(), array('FailOnAccessError'=>true), $user);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;
if($success)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Redflag OAuth client results</title>
</head>
<body>
<?php
echo '<h1>', HtmlSpecialChars($user->user->first_name),
' you have logged in successfully with Bitbucket!</h1>';
echo '<pre>', HtmlSpecialChars(print_r($user, 1)), '</pre>';

print_r($success);

?>
</body>
</html>
<?php
}
else
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<pre>Error: <?php echo HtmlSpecialChars($client->error); ?></pre>
</body>
</html>
<?php
}

?>

  4. Re: Very basic Oauth usage   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-08-03 03:10:30 - In reply to message 2 from Manuel Lemos
That is just an example. You need to remove the parts that you are not interested, like that of displaying the results.