LinkedIn SDK for Android

android
Mobile Apps
project

A lightweight android library to implement Login with LinkedIn in your Android app.

Why this UnOfficial SDK?

- Existing SDKs have been discontinued. Read More >>

- Official docs on developer.linkedin.com are outdated.

- Weird JSON returned by the new APIs. See following example:

{
    "handle": "urn:li:emailAddress:3775708763",
    "handle~": {
        "emailAddress": "[email protected]"
    }
}

Variable names in JAVA cannot have ~, so you cannot use dynamic JSON parsers OR networking libraries like Retrofit.


Adding the SDK to your Project

Method 1: Using JCenter

Just add the dependency to your app level build.gradle file

dependencies {
    implementation 'com.shantanudeshmukh:linkedinsdk:1.0.0'
}

If you are getting Failed to resolve ERROR, make sure that Jcenter repository is added to your project level build.gradle file. This is done by default in recent versions of Android Studio.

Method 2: Using Jitpack

  1. Make sure the Jitpack repository is present in your project level build.gradle file
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add the dependency to your app level build.gradle file
dependencies {
    implementation 'com.github.shantanu-deshmukh:LinkedIn-SDK-Android:1:0'
}

Usage

Authenticating

  1. Add internet permission to your AndroidManifest.xml file if it's not already added.
<uses-permission android:name="android.permission.INTERNET" />
  1. Initiate Login Request. (You might want to do this on click of a login button)
LinkedInBuilder.getInstance(MainActivity.this)
        .setClientID("<YOUR_CLIENT_ID_HERE>")
        .setClientSecret("<YOUR_CLIENT_SECRET_HERE>")
        .setRedirectURI("<YOUR_REDIRECT_URL_HERE>")
        .authenticate(LINKEDIN_REQUEST_CODE);

You can download the official Sign In with LinkedIn button images from here

  1. Handling Result: the sdk returns LinkedInUser object which contains the result data.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == LINKEDIN_REQUEST_CODE && data != null) {
            if (resultCode == RESULT_OK) {
                //Successfully signed in
                LinkedInUser user = data.getParcelableExtra("social_login");

                //acessing user info
                Log.i("LinkedInLogin", user.getFirstName());

            } else {

                if (data.getIntExtra("err_code", 0) == LinkedInBuilder.ERROR_USER_DENIED) {
                    //Handle : user denied access to account

                } else if (data.getIntExtra("err_code", 0) == LinkedInBuilder.ERROR_FAILED) {
                    
                    //Handle : Error in API : see logcat output for details
                    Log.e("LINKEDIN ERROR", data.getStringExtra("err_message"));
                }
            }
        }

    }

Retrieving updated profile

LinkedInBuilder.retrieveBasicProfile(linkedInUser.getAccessToken(), linkedInUser.getAccessTokenExpiry(), new OnBasicProfileListener() {
                    @Override
                    public void onDataRetrievalStart() {
                        //show progress bar if required
                    }

                    @Override
                    public void onDataSuccess(LinkedInUser linkedInUser) {
                        //handle retrived data
                    }

                    @Override
                    public void onDataFailed(int errCode, String errMessage) {
                        //handle error
                    }
                });

LinkedInUser Class

Return Method Description
String getId() Returns LinkedIn user ID
String getEmail() Returns users email (May return null)
String getFirstName() Returns first name of the user
String getLastName() Returns last name of the user
String getProfileUrl() Returns profile url of the user
String getAccessToken() Returns access token that can be used to retrive data later. You might want to store it for later use.
long getAccessTokenExpiry() Expiry timestamp of the access token

Security
To protect against CSRF during authorization, the sdk uses a 16 character token by default. If you want to use your own CSRF token, then use the setState method of the LinkedInBuilder class.

LinkedInBuilder.getInstance(MainActivity.this)
        .setClientID("<YOUR_CLIENT_ID_HERE>")
        .setClientSecret("<YOUR_CLIENT_SECRET_HERE>")
        .setRedirectURI("<YOUR_REDIRECT_URL_HERE>")
        .setState("<YOUR_CSRF_TOKEN_HERE>")
        .authenticate(LINKEDIN_REQUEST_CODE);