Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Integrate LinkedIn with Android Apps

DZone's Guide to

How to Integrate LinkedIn with Android Apps

LinkedIn is an important part of professional networking. Integrating it into your Android app could prove very beneficial. Check out how you can do so by reading this article.

· Mobile Zone
Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

LinkedIn has been coined as the world's largest professional network. It was one of the top ranking social media sites designed specifically for business communities. There has been a constant gaining of a popularity of the various social media platforms and with this rise in trend mobile app developers have grilled in further to integrate LinkedIn seamlessly with other applications. It has myriad advantages the most important of them being to allow users to share various feeds without exiting from the applications.

Image title

Today we would primarily talk about the implementation of LinkedIn integration with Android.

Step 1:Android SDK For LinkedIn

To get started, you first need to lay your hands on the latest version of LinkedIn’s Mobile SDK for Android and download it.

Step 2: LinkedIn APK

Official LinkedIn Android application is needed to support the mobile SDK of Android. For lucid development and easy debugging, you will require the LinkedIn APK file and get them installed manually on virtual devices.

Step 3: Setting up Development Environment

  • Download and Install IDE : The most popular IDE choices for this kind of integration implementation are Eclipse with various Android developer tools plugins, there are other options like Google’s Android Studio and JetBrains IntelliJ IDEA.
  • Installing Android SDK dependencies : You need to have Android SDK installed in your environment along with a build in tools if you are working on a sample application.

Step 4: Developing Sample Application

You have to follow the below-listed steps to build the LinkedIn Mobile SDK.

  • Launch Android Studio and select “Import Non- Android Studio Project”.
  • Next, you have to select the build.gradle file in the root of Mobile SDK for Android.
  • From the navigation bar select Build and then subsequently make project.

Precautionary: You can download the LinkedIn APK file using the emulated device browser if you plan on running your sample apps on the emulated Android device.

Step 5: Integrating LinkedIn App With Android App

5.1.Generating Key Values:

You have the scope to add specific information about the LinkedIn application into the Android application making use of the LinkedIn Application Settings. However, there are two types of key hashes Debug and Release.

Debug key is needed by LinkedIn to verify the authenticity of application whereas Release Key is a must need as all Android application must be signed prior to getting uploaded in the Play store.

5.2: Generating Debug Key Hash Value:

Windows

keytool -exportcert -keystore %HOMEPATH%\.android\debug.keystore -alias androiddebugkey | openssl sha1 -binary | openssl base64

Mac/Unix

keytool -exportcert -keystore ~/.android/debug.keystore -alias androiddebugkey | openssl sha1 -binary | openssl base64

5.3 Generating a Release Key Hash Value:

keytool -exportcert -keystore YOUR_RELEASE_KEY_PATH -alias YOUR_RELEASE_KEY_ALIAS | openssl sha1 -binary | openssl base64

5.4 Authenticating Mobile Users:

Before using the actual functionality of mobile SDK, you must create an authenticated LinkedIn session.

5.4.1: Initialization

Let’s see how to maintain an initialization connection to LinkedIn.

There are two ways:

  • Without an existing access token
  • With a previously acquired access token

5.4.1.1. Initialization by Requesting a New Access Token

The code is as follows:

java
public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        // Store a reference to the current activity
        final Activity thisActivity = this;

        LISessionManager.getInstance(getApplicationContext()).init(thisActivity, buildScope(), new AuthListener() {
            @Override
            public void onAuthSuccess() {
                // Authentication was successful.  You can now do
                // other calls with the SDK.
            }

            @Override
            public void onAuthError(LIAuthError error) {
                // Handle authentication errors
            }
        }, true);
    }
}

// Build the list of member permissions our LinkedIn session requires
private static Scope buildScope() {
    return Scope.build(Scope.R_BASICPROFILE, Scope.W_SHARE);
}

5.4.1.2: Initialization With an Existing Access Token

java
LISessionManager.getInstance(getApplicationContext()).init(AccessToken accessToken);

5.4.2 Handle Responses From LinkedIn Mobile App

While you are making requests to the LinkedIn app it will simultaneously validate if your app needs to handle the responses derived by calling LISessionManager’s implementation  ofonActivityResult()  via calling  ActivityResult()  method.

java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Add this line to your existing onActivityResult() method
    LISessionManager.getInstance(getApplicationContext()).onActivityResult(this, requestCode, resultCode, data);
}

You realize your authentication workflow completion as soon as your application starts exchanging messages from LinkedIn Mobile App successfully.

5.5 Authenticated REST API calls

Once you are set and got all the users authenticated, you can now make calls to LinkedIn’s REST API. Let’s have a rough look

5.5.1 APIHelper.getRequest()

java
String url = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name)";

APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
apiHelper.getRequest(this, url, new ApiListener() {
    @Override
    public void onApiSuccess(ApiResponse apiResponse) {
        // Success!
    }

    @Override
    public void onApiError(LIApiError liApiError) {
        // Error making GET request!
    }
});

5.5.2 APIHelper.postRequest()

java
String url = "https://api.linkedin.com/v1/people/~/shares";

JSONObject body = new JSONObject("{" +
    "\"comment\": \"Sample share\"," +
    "\"visibility\": { \"code\": \"anyone\" }," +
    "\"content\": { " +
        "\"title\": \"Sample share\"," +
        "\"description\": \"Testing the mobile SDK call wrapper!\"," +
        "\"submitted-url\": \"http://www.example.com/\"," +
        "\"submitted-image-url\": \"http://www.example.com/pic.jpg\"" +
    "}" +
"}");

APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
apiHelper.postRequest(this, url, body, new ApiListener() {
    @Override
    public void onApiSuccess(ApiResponse apiResponse) {
        // Success!
    }

    @Override
    public void onApiError(LIApiError liApiError) {
        // Error making POST request!
    }
});

5.5.3 APIHelper.putRequest()

java
String url = "https://api.linkedin.com/v1/companies/1234/updates/key=ABCDE-123456/update-comments-as-company/";

JSONObject body = new JSONObject("{" +
    "\"comment\": \"Sample share\" +
"}");

APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
apiHelper.putRequest(this, url, body, new ApiListener() {
    @Override
    public void onApiSuccess(ApiResponse apiResponse) {
        // Success!
    }

    @Override
    public void onApiError(LIApiError liApiError) {
        // Error making POST request!
    }
});

5.6 Cancelling In-progress Requests

There is also a provision to cancel in-progress API requests and it can be accomplished by calling APIHelper.cancelCalls()  method.

java
APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
apiHelper.cancelCalls(this);

These are the five simple steps to integrate LinkedIn with your android mobile apps. There is a similar process involving the same issue with iOS and we are going to discuss in the coming blogs. Stay tuned like your guitar!

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:
linkedin ,mobile apps ,android apps ,integration ,mobile ,ios apps ,app development ,app development company

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}