Background
While creating photo apps in Android, it is a frequent requirement to access photo albums from Facebook and
Twitter. Although there are SDKs to provide these functionalities but there
implementation is cumbersome. An open source SDK available that is easy to
use and allows integrating with not only Facebook and Twitter but also with several
social networks. This SDK is SocialAuth Android SDK. The SDK helps you to get albums from
Facebook and Twitter in a simple way along with examples.
Facebook stores all the posted images
in form of PhotoAlbums but Twitter not support this mechanism. Therefore for
Twitter, the API searches last 100
tweets from people you are following and create an album for it. For example, suppose
you are following Maxim. The API will create an album with the name Maxim and load
all images posted by Maxim in that album.
Getting Started
First you need to register your application with the
social provider you need to integrate, and get the API keys and secrets.
The following links will help you to get the API keys and secrets:
Integrating SDK
Now we can start integrating the SDK which can be
downloaded from http://code.google.com/p/socialauth-android. This SDK contains the Java libraries that do the heavy
lifting of OAuth as well as the REST calls for each social provider.
Extract the contents and we are all set.
- Copy libs/socialauth-android-3.0.jar and libs/socialauth4.3 jar
into the libs folder of your application. If you have latest ADT, jars will be automatically added to your build path else
you need to manually add the jar files in build path. We recommend you to upgrade to the latest ADT.
- Copy the
assets/oauth_consumer.properties file to the assets folder of your application. The file is used by the SDK for the keys and secrets. Replace
the default keys and secrets with your keys and secrets that were obtained above.
- Add android.permission.INTERNET and android.permission.ACCESS_NETWORK_STATE in manifest.
That is all you need to get started. The SDK zip file also contains code samples which can help you go ahead with the coding.
Code
Initalization
Initialize SocialAuthAdapter Object and create a Facebook /Twitter button. On clicking the button authorize the adapter. The code show how to authorize
the adapter for Twitter:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new SocialAuthAdapter(new ResponseListener());
twitter_button = (Button)findViewById(R.id.fb_btn);
twitter_button.setBackgroundResource(R.drawable.twitter);
twitter_button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
adapter.authorize(ProviderUI.this, Provider.TWITTER);
}
});
}
Getting UserAlbums
After authentication, you will receive response in ResponseListener. Here you can initiate call to get user
albums by getAlbumsAsync()
method and receive response in albumDataListener
. You can get album name, cover photo,
photos count , array of images with their name and URLs.
private final class ResponseListener implements DialogListener
{
public void onComplete(Bundle values) {
adapter.getAlbumsAsync(new AlbumDataListener());
}
private final class AlbumDataListener implements SocialAuthListener<List<Album>>
{
@Override
public void onExecute(List<Album> t) {
Log.d("Custom-UI", "Receiving Data");
List<Album> albumList = t;
if (albumList != null && albumList.size() > 0) {
for (Album a : albumList) {
Log.d("Custom-UI", "Album ID = " + a.getId());
Log.d("Custom-UI", "Album Name = " + a.getName());
Log.d("Custom-UI", "Cover Photo = " + a.getCoverPhoto());
Log.d("Custom-UI", "Photos Count = " + a.getPhotosCount());
photosList = a.getPhotos();
if (photosList != null && photosList.size() > 0) {
for (Photo p : photosList) {
Log.d("Custom-UI", "Photo ID = " + p.getId());
Log.d("Custom-UI", "Name = " + p.getTitle());
Log.d("Custom-UI", "Thumb Image =" + p.getThumbImage());
Log.d("Custom-UI", "Small Image =" + p.getSmallImage());
Log.d("Custom-UI", "Medium Image =" + p.getMediumImage());
Log.d("Custom-UI", "Large Image = " + p.getLargeImage());
}}
}}
This same code work for Facebook too. You just need to use Provider.Facebook
to load albums from Facebook.
Download
To download the SDK and examples,
please visit: http://code.google.com/p/socialauth-android.