Category: oAuth

Using OAuth2 to make authenticated calls to WP REST API

clueless

WP REST API is a cool solution for the developers who want to interact with their WordPress blogs programmatically. It’s handy and pretty useful. However, the documentation lacks some working examples which makes a lot of people confused. I’ve started to document some working examples at http://wpapi.xyz. The list is small, but it’s growing.

Anyway, I’ve made a small video tutorial last night to demonstrate how to authenticated yourself using OAuth2 and make some authenticated calls to your WP REST API endpoint. There are hundreds of confused users floating out there looking for a solution, and I hope this video will fix it. Let me know if you find it useful

Enjoy!

Here comes Postman, the tiny little app that connects Flickr and Facebook Groups together

Postman Flickr to Facebook Groups photo sharing service

We were working to develop a new utility web application for last couple of days which will help us, the photographers, to easily share photos from our flickr photostream to multiple Facebook groups that we are subscribed to. There was a reason why we wanted to develop this app in the first place. Every time we had to upload the same photo to each of these groups and needless to say that Facebook’s image compression routine still thinks that we live in medieval age, probably King Arthur’s reign. Anyway, the process is boring and time consuming. Another quick and dirty way was to share the photo by Facebook’s group email feature but not every group supports it. So we had to find a way to make it easy for the greater part who uses both Flickr and Facebook to share their photos.

So we’ve developed Postman, a nice little app which does the job very well. Here is what/how it works.

  • Authenticate users against Flickr and Facebook and gathers necessarily permissions (read for Flickr, publish_stream and user_groups for Facebook Connect)
  • Display last 100 photos from user’s Flickr stream in a selectable grid, with the option to load more.
  • Download the 1024px version of the selected photo from Flickr Stream to our server
  • Post that photo to selected Facebook groups using FB PHP-SDK one by one.
  • Delete the photo from server

It actually solved the big problem for us. Postman made it easy for everyone of us (the photographers) and everyone literally loved it. There is one more reason to love WordPress and that is Flickr’s compression algo is really good. So when we download the 1024px version of user’s photo and then push it to Facebook, luckily Facebook  doesn’t do much on that and the photo looks really good. And we used AppFog to host Postman which ultimately proved to be very good choice.

If you are a photographer who uses Flickr to host his photos and then share to multiple Facebook groups, please give POSTMAN a try and we are sure that you will love it.

And last but not the least, Postman can post photos to your Facebook pages too. So Postman is a handy application photographers, by photographers. Use it and enjoy. You can find postman at http://postman.im

Shameless Plug

We develop beautiful Admin Panel templates for the web application developers. Our recent product is “Bolt” which is responsive and built on top of Twitter’s bootstrap. Give it a try and let us know how do you like it.

Bolt Responsive Admin Panel Template for Developers
Bolt Responsive Admin Panel Template for Developers

connecting to flickr using PHP and PECL oAuth extension

flickr, one of the world dominant in photo sharing service, has added support to oAuth recently. Though their old authentication system still works (marked as deprecated but not discontinued) I find it’s wise to use their oAuth system for the better future proof application development.

PHP has a nice extension to perform oAuth dance in it’s pecl repository – pecl oAuth. Here is the code to connect to flickr using this extension. The only catch I found and took me more than 30 minutes to figure out a failed attempt, is you will have to append the permission flag in it’s oAuth authorization url. Pass either one of these permission flags “read”,”write” or “delete” as “&perms=” (flickr.php, line # 20) and then the redirection will be successful. Register your Flickr application from here, you will find your consumer key and secret key in next page in the App Garden.

While creating new application in flickr app garden, point the callback url to your flickr.php – thats it.

source code of config.php
[sourcecode lang=”php”]
<?php
$oauth[‘flickr’][‘consumerkey’]="9e251c5bcc*********cac050bf50ef";
$oauth[‘flickr’][‘consumersecret’]="d1c057904945****";
$oauth[‘flickr’][‘requesttokenurl’]="http://www.flickr.com/services/oauth/request_token";
$oauth[‘flickr’][‘accesstokenurl’]="http://www.flickr.com/services/oauth/access_token";
$oauth[‘flickr’][‘authurl’]="http://www.flickr.com/services/oauth/authorize";
?>
[/sourcecode]

And here is the source code of flickr.php
[sourcecode lang=”php”]
<?php
/**
* flickr authentication script based on
* pecl oauth extension
*/
session_start();
include_once("config.php");
/*
unset($_SESSION[‘frequest_token_secret’]);
unset($_SESSION[‘faccess_oauth_token’]);
unset($_SESSION[‘faccess_oauth_token_secret’]);
*/
$oauthc = new OAuth($oauth[‘flickr’][‘consumerkey’],
$oauth[‘flickr’][‘consumersecret’],
OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI); //initiate
if(empty($_SESSION[‘frequest_token_secret’])) {
//get the request token and store it
$request_token_info = $oauthc->getRequestToken($oauth[‘flickr’][‘requesttokenurl’]); //get request token
$_SESSION[‘frequest_token_secret’] = $request_token_info[‘oauth_token_secret’];
header("Location: {$oauth[‘flickr’][‘authurl’]}?oauth_token=".$request_token_info[‘oauth_token’]."&perms=read");//forward user to authorize url with appropriate permission flag
}
else if(empty($_SESSION[‘faccess_oauth_token’])) {
//get the access token – dont forget to save it
$request_token_secret = $_SESSION[‘frequest_token_secret’];
$oauthc->setToken($_REQUEST[‘oauth_token’],$request_token_secret);//user allowed the app, so u
$access_token_info = $oauthc->getAccessToken($oauth[‘flickr’][‘accesstokenurl’]);
$_SESSION[‘faccess_oauth_token’]= $access_token_info[‘oauth_token’];
$_SESSION[‘faccess_oauth_token_secret’]= $access_token_info[‘oauth_token_secret’];
}
if(isset($_SESSION[‘faccess_oauth_token’])) {
//now fetch current users profile
$access_token = $_SESSION[‘faccess_oauth_token’];
$access_token_secret =$_SESSION[‘faccess_oauth_token_secret’];
$oauthc->setToken($access_token,$access_token_secret);
$data = $oauthc->fetch(‘http://api.flickr.com/services/rest/?method=flickr.test.login&api_key=ae29ce34e831937ac26483498e93f3e9&format=json’);
$response_info = $oauthc->getLastResponse();
echo "<pre>";
print_r(json_decode($response_info));
echo "</pre>";
}
?>
[/sourcecode]

You can download the complete package from here (Thanks Box.net)

Note: I have written a similar post to demonstrate connecting to twitter and linkedin via their oAuth protocol which you can find here 🙂