Reputation: 485
I am trying to make a connection to an API that will provide my website with information about cars and other stuff. The problem is that I can connect with it from PostMan because it generates a signature and a nounce. The problem that I am suffering is that when I want to connect throw PHP the OAUTH class is not generating the nounce and the signature so I can´y connect. I have also tried with CURL and anything better happened.
My current code is this:
$config = array(
'consumer_key' => 'XXXXXXXXXXXXXXXXXXXXXXX',
'consumer_secret' => 'XXXXXXXXXXXXXXXXXXXXXXX',
'request_uri' => 'http://bdc.autobild.es/autobild'
);
$oauth = new OAuth($config['consumer_key'],
$config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1);
$requestTokenInfo = $oauth->getRequestToken('http://bdc.autobild.es/autobild/','http://localhost/myfolder/');
d($requestTokenInfo);
$accessTokenInfo = $oauth->getAccessToken('http://bdc.autobild.es/autobild/');
d($accessTokenInfo);
$requestInfo = $oauth->getLastResponseInfo();
d($requestInfo);
$oauth->fetch("http://bdc.autobild.es/autobild/list?type=coche");
$data = $oauth->getLastResponseInfo();
d($data);
Thanks!
Upvotes: 2
Views: 6022
Reputation: 111
You can use another library, i recommand you tu use GuzzleHttp. To install it you can use Composer .
{ "require": { "guzzlehttp/guzzle": "6.5", "guzzlehttp/oauth-subscriber": "^0.6.0" } }
And for implementation you can process like this
use \GuzzleHttp\Client;
use \GuzzleHttp\HandlerStack;
use \GuzzleHttp\Subscriber\Oauth\Oauth1;
$stack = HandlerStack::create();
$middleware = new Oauth1(['consumer_key' => '','consumer_secret' => '','signature_method' => Oauth1::SIGNATURE_METHOD_HMAC]);
$stack->push($middleware);
$client = new Client(['base_uri' => '<BASE_URL>','handler' => $stack]);
$res = $client->get('<ROUTE_DATA>', ['auth' => 'oauth'])->getBody()->getContents();
$res = json_decode($res);
var_dump($res->title);
Upvotes: 0
Reputation: 485
I have found the solution. The problem was that an argument was missing here:
$oauth = new OAuth($config['consumer_key'], $config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1);
The correct way is:
$oauth = new OAuth($config['consumer_key'], $config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
So, the complete code is like this:
$config = array(
'consumer_key' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
'consumer_secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
);
$oauth = new OAuth($config['consumer_key'], $config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$requestInfo = $oauth->getLastResponseInfo();
$oauth->fetch("http://example.es/list?type=car");
$data = $oauth->getLastResponse();
var_dump($data);
Thank´s everyone for the help!
Upvotes: 1
Reputation: 40730
Here's how I authenticate to an OAuth service, maybe it will be useful:
signin.php
//Gets the request token
session_start();
$oauth = new OAuth($config['consumer_key'], $config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);
$requestTokenInfo = $oauth->getRequestToken("<oauth token URL>", $_SERVER["SERVER_NAME"]."/callback.php");
$_SESSION["tokenInfo"] = $requestTokenInfo;
header("Location: <oauth authorize URL>?token='.$requestTokenInfo["oauth_token"]); //May vary on your OAuth service
In callback.php
session_start();
$oauth = new OAuth($config['consumer_key'], $config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);
$oauth->setToken($_REQUEST['oauth_token'], $_SESSION["tokenInfo"]["secret"]);
$accessTokenInfo = $oauth->getAccessToken("<oauth authorize URL>");
$_SESSION["accessToken"] = $accessTokenInfo;
unset($_SESSION["tokenInfo"]); //Its work is done
Now whenever I need to get some API data:
session_start();
$oauth = new OAuth($config["consumer_key"], $config["consumer_secret"], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->setToken($_SESSION["accessToken"]["token"],$_SESSION["accessToken"]["secret"]);
$oauth->fetch("URL");
$data = $oauth->getLastResponse();
$requestInfo = $oauth->getLastResponseInfo();
$responseHeaders = $oauth->getLastResponseHeaders();
This will work in the cases you need to perform requests on behalf of a user. The user will be redirected to the service's oauth autorizer and then returned to the callback page with their tokens if they authorize your app.
If the service has provided some methods which are accessible from your app directly (typically getting app usage information and the like is linked to these) then you can probably skip a big portion of this:
$oauth = new OAuth($config['consumer_key'], $config['consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1);
$oauth->fetch(url);
Upvotes: 3