Part 2: Using SocialAuth to Integrate LinkedIn API in Android

LinkedIn is one of the most popular social networking platforms available today. LinkedIn users can share status updates, respond to topics of interest, read the latest updates from contacts and companies, and participate in group discussions about topics that interest them. The popularity of social networking platforms like Facebook, LinkedIn, and Twitter means that mobile app developers are tightly integrating these services with their applications to allow users to share content on these sites without ever leaving an application. Although there are solutions available to integrate LinkedIn with Android applications , the implementation process is quite difficult and restricts use with other social networks.

SocialAuth Android is an easy to use open source SDK that allows mobile developers to create apps that integrate with LinkedIn. It provides simple code to access its various functionalities, along with free support, and it is one of the few SDKs that can help integrate other social networks in your Android app, along with LinkedIn.

Using this SDK you can share your status, get user profile and list of connections.

Getting Started

First you need to register your application with LinkedIn, and get the API keys and secret keys. The following link will help you to get these: https://code.google.com/p/socialauth-android/wiki/Linkedin

Integrate SDK in App

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.

  1. Copy the libs/socialauth-android-2.5.jar and libs/socialauth4.2 jar into the libs folder of your application. If you have the latest Android Development Tools (ADT), jars will be automatically added to your build path. If not, you will need to manually add the jar files in build path. We recommend you upgrade to the latest ADT.
  2. Copy the assets/oauth_consumer.properties file to the assets folder of your application. The file is used by the SDK for the API and secret keys. Replace the default entries with the keys generated by following the above steps.
  3. 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.

Implementation

You just need to click button and it will open a LinkedIn login dialog box. After user authorization is completed you can get options to various functionalities.

LinkedIn login dialog box

 

Initialization

Initialize the SocialAuth Adapter Object and create a LinkedIn button. After clicking the button, give the required authorization to the adapter.

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
       adapter = new SocialAuthAdapter(new ResponseListener());
        
      linkedin_button = (Button)findViewById(R.id.fb_btn);
      linkedin_button.setBackgroundResource(R.drawable.linkedin);
     
     facebook_button.setOnClickListener(new OnClickListener() 
     {
        public void onClick(View v) 
        {
            adapter.authorize(ProviderUI.this, Provider.LINKEDIN);
        }
    });
    }

Receive Authentication response in ResponseListener and initiate calls to get various functionalities.  See Examples below:

Update Status

Share your status by calling adapter.updateStatus() and verify response in MessageListener. You can share your status on your LinkedIn account in any language.

private final class ResponseListener implements DialogListener 
{
   public void onComplete(Bundle values) {
   
      adapter.updateStatus(edit.getText().toString(), new MessageListener());                   
   }
}

// To get status of message after authentication
private final class MessageListener implements SocialAuthListener {
    @Override
    public void onExecute(Integer t) {

    Integer status = t;
    if (status.intValue() == 200 || status.intValue() == 201 ||status.intValue() == 204)
     Toast.makeText(CustomUI.this, "Message posted",Toast.LENGTH_LONG).show();
    else
    Toast.makeText(CustomUI.this, "Message not posted",Toast.LENGTH_LONG).show();
   }
}


Get Profile

Get User profile by getUserprofileAsync() method and receive response in profileDataListener. You can get

User profile ID, first name , last name , email, country, language , location and profile image URL for LinkedIn.

private final class ResponseListener implements DialogListener 
{
   public void onComplete(Bundle values) {
   
      adapter.getUserProfileAsync(new ProfileDataListener());                   
   }
}

// To receive the profile response after authentication
private final class ProfileDataListener implements SocialAuthListener {

@Override
public void onExecute(Profile t) {

   Log.d("Custom-UI", "Receiving Data");
   Profile profileMap = t;
   Log.d("Custom-UI",  "Validate ID         = " + profileMap.getValidatedId());
   Log.d("Custom-UI",  "First Name          = " + profileMap.getFirstName());
   Log.d("Custom-UI",  "Last Name           = " + profileMap.getLastName());
   Log.d("Custom-UI",  "Email               = " + profileMap.getEmail());
   Log.d("Custom-UI",  "Country                  = " + profileMap.getCountry());
   Log.d("Custom-UI",  "Language                 = " + profileMap.getLanguage());
   Log.d("Custom-UI",  "Location                 = " + profileMap.getLocation());
   Log.d("Custom-UI",  "Profile Image URL  = " + profileMap.getProfileImageURL());


Get Contacts

Get User contacts by getContactListAsync() method and receive response in contactDataListener. You can get first name, last name and profile URL for LinkedIn.

private final class ResponseListener implements DialogListener 
{
   public void onComplete(Bundle values) {
   
      adapter.getContactListAsync(new ContactDataListener());                   
   }
}

// To receive the contacts response after authentication
private final class ContactDataListener implements SocialAuthListener> {

@Override
public void onExecute(List t) {

   Log.d("Custom-UI", "Receiving Data");
   List contactsList = t;
  if (contactsList != null && contactsList.size() > 0) {
  for (Contact c : contactsList) {
    Log.d("Custom-UI", "Contact ID = " + c.getId());
    Log.d("Custom-UI", "First Name = " + c.getFirstName());
    Log.d("Custom-UI", "Last Name = " + c.getLastName());
    }
}


Conclusion

That’s the overview of how to download and install SocialAuth Android to easily integrate LinkedIn with your Android applications. For the next and last post in the series, we’ll cover how to integrate the Twitter API in Android applications using SocialAuth. If you have questions or need support, feel free to leave comments on the main SocialAuth Android page.