Jump to content

Get data from external API with Oauth


Robin S
 Share

Recommended Posts

I need to retrieve some data from an external API that uses Oauth 1.0 authentication. This is the first time I've needed to use Oauth so I'm looking for advice.

Does PW have any built-in tools that help with retrieving data from an external API and using Oauth? WireHttp maybe?

For doing the Oauth stuff is it sensible to use a dedicated library (like OAuth 1.0 Client from thephpleague) or is it easy enough with just the functions built into PHP and/or PW?

  • Like 2
Link to comment
Share on other sites

Just reporting back that for my case this turned out to be very easy using just WireHttp alone and no Oauth client library needed.

$http = new WireHttp();

$consumer_key = 'my_key';
$consumer_secret = 'my_secret';
$auth_token = 'my_auth_token';
$auth_token_secret = 'my_auth_token_secret';
$time = time();
$nonce = md5(uniqid(microtime()));

// OAuth authorization header
$http->setHeader('Authorization', "OAuth oauth_consumer_key=$consumer_key, oauth_token=$auth_token, oauth_version=1.0, oauth_timestamp=$time, oauth_nonce=$nonce, oauth_signature_method=PLAINTEXT, oauth_signature={$consumer_secret}%26{$auth_token_secret}");

// Parameters
$params = [
    'member_listing'    => '123456',
    'rows'              => '20',
    'photo_size'        => 'FullSize',
    'return_metadata'   => 'false',
];
$params_str = http_build_query($params);

// Get JSON response from Trade Me
$json = $http->getJSON("https://api.tmsandbox.co.nz/v1/Search/Property/Rental.json?$params_str");

Because of the circumstances of my case I can hardcode $auth_token and $auth_token_secret. But more often with OAuth you would need to get these values in a separate query and store them.

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...