Saturday 16 February 2013

How to integrate Facebook & Linkedin in Android Apps


This article helps users to integrate Facebook & Linkedin functionality easily in Android apps.




Background
While we creating Android apps, it is again and again requirement to be able to post status updates on social networks like Facebook & LinkedIn. The user experience is that users are first taken to a page where they can login to Facebook or LinkedIn. Then the control returns back to the application which can now post messages on their behalf. 

There are multiple ways of doing the integration. One is to use the SDKs available from Facebook, Linkedin etc. which provide the API for sharing these. Second is to use open an embedded browser control and use OAuth for authentication and finally the REST APIs provided to post the update. Both of these are slightly tedious, since either you have to download and use the different APIs or you have to implement the complete protocol.
  
Fortunately there is an open source SDK available that is easy to use and allows integrating with several social networks. This is known as SocialAuth Android SDK. In addition to posting


status updates, you can also get user profile from Facebook, Twitter and LinkedIn using the API, which will allow you to register users as well. 

Getting Started for Facebook

First you need to register your application with the social provider you need to integrate, and get the API key and secret.

Step1. Go to URL - http://www.facebook.com/developers and fill username and password. Create account if you don't have one.





Step2. After creation of account, this page will open - https://developers.facebook.com/apps

Step 3. Create new App and Fill all details as shown below. Fill dummy url in site url field.

4. Note down keys and secrets and copy them in oauth_consumers.properties file.

Integrate SDK in App

Now we can start integrating the SDK which can be downloaded from Download. 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 the libs/socialauth-vincent.jar and libs/socialauth 2.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 key and secret with your key and secret that were obtained above. 

Add android.permission.INTERNET and android.permission.ACCESS_NETWORK_STATE in manifest. 

Implementation

Create a button in your application and the behavior on click is supplied by the SDK through a SocialAuthAdapter object.

Following is the code for creating the adapter and attaching it with the button. 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  
    Button btnshare = (Button)findViewById(R.id.btnshare);
    btnshare.setText("Share");
    adapter = new SocialAuthAdapter(new ResponseListener());
    adapter.addProvider(Provider.FACEBOOK, R.drawable.facebook);
    adapter.enable(btnshare);
}

You also need to provide a ResponseListener to do the handling once the authentication is complete, for example, to post updates

private final class ResponseListener implements DialogListener 
{
     public void onComplete(Bundle values) {
         Log.d("ShareButton" , "Authentication Successful");
                   
        adapter.updateStatus("http://www.vincentit.com/ "); 
} 

Getting Started for Linkedin

First you need to register your application with the social provider you need to integrate, and get the API key and secret.
Step1. Go to url : https://www.linkedin.com/ and create your account.


Step2. Go to URL: http://developer.linkedin.com/ and press Sign In.

Click on Sign In then you got this window:

Enter the Email and Password, and click on OK, I’ll Allow It.
You got another same window as previous:

Click on Support and select API Keys.
You got next screen as:

Click Add New Application
Next window open as:


Fill all the necessary fields, checked on  LinkedIn API Terms of Use and clicked on Add Application.

Note down keys and secrets and copy them in oauth_consumers.properties file.

Integrate SDK in App

Now we can start integrating the SDK which can be downloaded from Download. 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 the libs/socialauth-vincent.jar and libs/socialauth 2.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 key and secret with your key and secret that was obtained above. 

Add android.permission.INTERNET and android.permission.ACCESS_NETWORK_STATE in manifest. 

Implementation

Create a button in your application and the behavior on click is supplied by the SDK through a SocialAuthAdapter object.

Following is the code for creating the adapter and attaching it with the button. 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  
    Button btnshare = (Button)findViewById(R.id.btnshare);
    btnshare.setText("Share");
    adapter = new SocialAuthAdapter(new ResponseListener());
    adapter.addProvider(Provider.FACEBOOK, R.drawable.facebook);
    adapter.enable(btnshare);
}

You also need to provide a ResponseListener to do the handling once the authentication is complete, for example, to post updates

private final class ResponseListener implements DialogListener {
     public void onComplete(Bundle values) {
         Log.d("ShareButton" , "Authentication Successful");
                   
        adapter.updateStatus("http://www.vincentit.com/ "); 
}