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.

Vineet Aggarwal

Vineet Aggarwal

Sr. Technical Lead

Vineet Aggarwal is a Sr. Technical Lead at 3Pillar Global Labs, which is part of 3Pillar’s company strategy in the fields of big data analytics, mobility, and cloud-based applications. Vineet leads development and support for the popular open source tool SocialAuth–Android. He also leads our Android Competency Center in India and is responsible for mentoring Android Developers across the company to develop internal expertise. He has hands-on experience with various mobile platforms and keen interest in Android and iPhone. He loves designing new apps, learning upcoming technologies, and mentoring members of the Android community.

6 Responses to “Part 2: Using SocialAuth to Integrate LinkedIn API in Android”
  1. Ashish on

    Hi Vineet,

    I have integrated Facebook, Twitter in my android native app. both are working fine as expected but when i integrated Linkedin with Socialauth (same) library then getting an error like “org.brickred.socialauth.android.SocialAuthError: URL Authentication error” I googled lot but could not find a solution. I am using socialauth-4.8.jar and socialauth-android-3.2.jar files.

    your help will be appreciated.

    regards
    Ashish C

    Reply
    • archit on

      @ashish
      hi i am trying to integrate facebook SDK,in my login i am not getting the gradle build in the facebook SDK file could you please suggest url form where you downloaded facebook SDK
      and how you unzipped the file.

      and trying with twitter integration in this so many error are coming in my project.
      using
      socialauth-4.4.jar
      socialauth-android-3.2.zip files
      not getting 3.2.jar file

      Reply
      • Zain Ali on

        i am facing the same problem and i solve it by following steps.
        1)file new module
        then enter the path it will give u error if u using android studio not eclipse so to need to select the rar file from the sdk folder then .
        if this solve in your case then file other wise make a new solder in project directory and copy the file of sdk there and then in setting of gradle include the file name. so it is solved!
        for further query email me

        Reply
  2. Himansu on

    Hi Vineet,

    Thanks for the nice tutorial. It’s very helpful!

    I am facing one issue while fetching LinkedIn contact list. Below is the error I am getting;
    “org.brickred.socialauth.exception.ServerDataException: Failed to parse the contacts from response.http://api.linkedin.com/v1/people/~/connections:(id,first-name,last-name,public-profile-url,picture-url)

    Would appreciate your help here.

    Thanks in advance!
    Himansu

    Reply
  3. Ujjwal on

    Hey Himanshu,
    I am getting the same error as you, its because contact list is not getting fetched and null. Seems like some API restriction created by LinkedIn recently. So, Vineet, do you have any solution to fix this please.

    Thanks

    Reply
  4. Suraj Sahijwani on

    Hi, is this incomplete code or what? Nothing happens on button click, I do not get error nor it asks for authentication with LinkedIn app. Please can you post full code for reference. Thanks!

    Reply
Leave a Reply

Related Posts

The 3 Keys to Building Products That Drive Retention –... I had the privilege of being invited to speak at the Wearable Technology Show in Santa Clara this week, where I gave a bit of a reprisal of a talk I d...
High Availability and Automatic Failover in Hadoop Hadoop in Brief Hadoop is one of the most popular sets of big data processing technologies/frameworks in use today. From Adobe and eBay to Facebook a...
3Pillar CEO David DeWolf Quoted in Enterprise Mobility Excha... David DeWolf, Founder and CEO of 3Pillar Global, was recently quoted in a report by Enterprise Mobility Exchange on the necessity of understanding and...
How the Right Tech Stack Fuels Innovation – The Innova... On this episode of The Innovation Engine podcast, we take a look at how choosing the right tech stack can fuel innovation in your company. We'll talk ...
The Road to AWS re:Invent 2018 – Weekly Predictions, P... For the last two weeks, I’ve been making predictions of what might be announced at AWS’ upcoming re:Invent conference. In week 1, I made some guesses ...