Today we are going to learn how to integrate LinkedIn SDK in Android. By integrating LinkedIn into Android your user will be able to SingIn very quickly.
In this tutorial we are going to learn following features:
1. Authenticating with LinkedIn.
2. Fetching User Basic profile.
3. Sharing post to LinkedIn.
4. Opening current profile in LinkedIn app.
5. Opening others profile in LinkedIn app.
6. Log Out or clearing Logged In user session.
Tech Tip : Get an hassle-free remote access to your essential android apps on any device(PC/android/iOS) by loading Bluestacks onto a hosted xendesktop available at an affordable xendesktop cost. Learn more about Hosted QuickBooks and other hosted software products by visiting Apps4Rent.
First step is to create an app in LinkedIn. To create app follow the below video:
Now to get Package Hash for created app use the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private void getPackageHash() { try { @SuppressLint("PackageManagerGetSignatures") PackageInfo info = getPackageManager().getPackageInfo( "com.package_name",//give your package name here PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d(TAG, "Hash : " + Base64.encodeToString(md.digest(), Base64.NO_WRAP));//Key hash is printing in Log } } catch (PackageManager.NameNotFoundException e) { Log.d(TAG, e.getMessage(), e); } catch (NoSuchAlgorithmException e) { Log.d(TAG, e.getMessage(), e); } } |
Place the above code inside your Activity and call it in onCreate and it will give you Package Hash which you can use to put inside created App settings.
Now download LinkedIn mobile SDK from this link. After downloading follow the below video to see how to add the LinkedIn SDK in android studio project.
After successfully adding SDK into your Android Studio project use the below code to authenticate account with LinkedIn.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
LISessionManager.getInstance(getApplicationContext()).init(this, buildScope()//pass the build scope here , new AuthListener() { @Override public void onAuthSuccess() { // Authentication was successful. You can now do // other calls with the SDK. Toast.makeText(MainActivity.this, "Successfully authenticated with LinkedIn.", Toast.LENGTH_SHORT).show(); } @Override public void onAuthError(LIAuthError error) { // Handle authentication errors Log.e(TAG, "Auth Error :" + error.toString()); Toast.makeText(MainActivity.this, "Failed to authenticate with LinkedIn. Please try again.", Toast.LENGTH_SHORT).show(); } }, true);//if TRUE then it will show dialog if // any device has no LinkedIn app installed to download app else won't show anything |
Now to handle the authentication response in your app use the below code:
1 2 3 4 5 6 7 |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Add this line to your existing onActivityResult() method LISessionManager.getInstance(getApplicationContext()).onActivityResult(this, requestCode, resultCode, data); } |
During the authentication process, there are several possible scenarios that could occur which result in different user experiences, based on:
After successful authentication you will be able to fetch user basic profile data by calling LinkedIn REST APIs. For fetching basic profile data you can use below code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
private void fetchBasicProfileData() { //In URL pass whatever data from user you want for more values check below link //LINK : https://developer.linkedin.com/docs/fields/basic-profile String url = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,public-profile-url,picture-url,email-address,picture-urls::(original))"; APIHelper apiHelper = APIHelper.getInstance(getApplicationContext()); apiHelper.getRequest(this, url, new ApiListener() { @Override public void onApiSuccess(ApiResponse apiResponse) { // Success! Log.d(TAG, "API Res : " + apiResponse.getResponseDataAsString() + "\n" + apiResponse.getResponseDataAsJson().toString()); Toast.makeText(MainActivity.this, "Successfully fetched LinkedIn profile data.", Toast.LENGTH_SHORT).show(); } @Override public void onApiError(LIApiError liApiError) { // Error making GET request! Log.e(TAG, "Fetch profile Error :" + liApiError.getLocalizedMessage()); Toast.makeText(MainActivity.this, "Failed to fetch basic profile data. Please try again.", Toast.LENGTH_SHORT).show(); } }); } |
To know more about basic profile parameters check this link.
To share some data to LinkedIn you have to call another REST Api by posting the sharing content to it. You can use below code to share content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
public void onShareClick() { //LINK : https://developer.linkedin.com/docs/share-on-linkedin String url = "https://api.linkedin.com/v1/people/~/shares"; JSONObject body = null; try { //prepare json object for sharing body = new JSONObject("{" + "\"comment\": \"Test Comment\"," + //A comment by the member to associated with the share. // If none of the above content parameters are provided, the comment must contain a URL to the content you want to share. If the comment contains multiple URLs, only the first one will be analyzed for content to share. "\"visibility\": { \"code\": \"anyone\" }," +//A collection of visibility information about the share. //One of the following values: // anyone: Share will be visible to all members. // connections-only: Share will only be visible to connections of the member performing the share. // This field is required in all sharing calls. "\"content\": { " + "\"title\": \"Test share\"," +//The title of the content being shared. "\"description\": \"Testing the mobile SDK share feature!\"," +//The description of the content being shared. "\"submitted-url\": \"https://www.androhub.com/\"," +//A fully qualified URL for the content being shared. "\"submitted-image-url\": \"https://androhub.com/wp-content/uploads/2016/05/andro_new.png\"" +//A fully qualified URL to a thumbnail image to accompany the shared content. The image should be at least 80 x 150px for best results. "}" + "}"); } catch (JSONException e) { e.printStackTrace(); } APIHelper apiHelper = APIHelper.getInstance(getApplicationContext()); apiHelper.postRequest(this, url, body, new ApiListener() { @Override public void onApiSuccess(ApiResponse apiResponse) { // Success! Log.d(TAG, "Share response : " + apiResponse.toString()); Toast.makeText(MainActivity.this, "Shared successfully.", Toast.LENGTH_SHORT).show(); } @Override public void onApiError(LIApiError liApiError) { // Error making POST request! Log.e(TAG, "Share error : " + liApiError.getLocalizedMessage()); Toast.makeText(MainActivity.this, "Failed to share. Please try again.", Toast.LENGTH_SHORT).show(); } }); } |
NOTE : LinkedIn doesn’t provide Image Sharing via Gallery or Camera to his application via SDK for this you can use Share Intent.
If you want to cancel any API call which is in-progress use the below code:
1 2 |
APIHelper apiHelper = APIHelper.getInstance(getApplicationContext()); apiHelper.cancelCalls(this); |
This method will open the currently authenticated user’s LinkedIn profile directly within the official LinkedIn mobile application. Use the below code to fetch current profile :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public void openMyProfile() { DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance(); // Open the current user's profile deepLinkHelper.openCurrentProfile(this, new DeepLinkListener() { @Override public void onDeepLinkSuccess() { // Successfully sent user to LinkedIn app Toast.makeText(MainActivity.this, "Current profile opened successfully.", Toast.LENGTH_SHORT).show(); } @Override public void onDeepLinkError(LIDeepLinkError error) { // Error sending user to LinkedIn app Log.e(TAG, "Current profile open error : " + error.toString()); Toast.makeText(MainActivity.this, "Failed to open current profile.", Toast.LENGTH_SHORT).show(); } }); } |
This method will open a specific member profile within the official LinkedIn application. The memberID is a value that is unique to your particular LinkedIn application, representing a specific LinkedIn member. You can retrieve the current user’s id by making a REST API call to LinkedIn’s Profile API using the SDK’s API wrapper method.
Storing member ID values on your own server allows your application to direct users to the official LinkedIn profiles for any other users that have authorised your mobile application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public void openOthersProfile(View view) { //A sample member ID value to open for this first you have to get user id final String targetID = "any_id"; DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance(); // Open the target LinkedIn member's profile deepLinkHelper.openOtherProfile(this, targetID, new DeepLinkListener() { @Override public void onDeepLinkSuccess() { // Successfully sent user to LinkedIn app Toast.makeText(MainActivity.this, "Other profile opened successfully.", Toast.LENGTH_SHORT).show(); } @Override public void onDeepLinkError(LIDeepLinkError error) { // Error sending user to LinkedIn app Log.e(TAG, "Other profile open error : " + error.toString()); Toast.makeText(MainActivity.this, "Failed to open other profile.", Toast.LENGTH_SHORT).show(); } }); } |
To handle DeepLinking callback add the below code in your Activity :
1 2 3 4 5 6 7 8 9 |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Add this line to your existing onActivityResult() method DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance(); deepLinkHelper.onActivityResult(this, requestCode, resultCode, data); } |
You can clear/logout user session when you don’t want it anymore by calling clearSession method like below:
1 |
LISessionManager.getInstance(this).clearSession(); |
1. Create an App on LinkedIn and get update Package Name and Package Hash over there. To know how to create App check the video given above in article.
2. After creating app download LinkedIn SDK and add it into your Android Studio project. Download the LinkedIn SDK from here.
3. After adding LinkedIn SDK and syncing project open activity_main.xml and add the below code to it. In this layout there are several buttons to perform different tasks like : SignIn, Share, Open Profile page and Logout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.android_linkedin_integration_demo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <!-- Button for SignIn --> <Button android:id="+id/linkedin_login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#1c5199" android:drawableLeft="drawable/linkedin_logo" android:drawableStart="drawable/linkedin_logo" android:gravity="center" android:onClick="signInWithLinkedIn" android:text="string/sign_in_with_linkedin" android:textColor="android:color/white" android:textSize="14sp" /> <!-- image view to show user profile image --> <ImageView android:id="+id/user_profile_image_view" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" /> <!-- text view to show user details --> <TextView android:id="+id/user_details_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="color/colorPrimary" android:textSize="15sp" /> <!-- logout button only visible after successful Sign In --> <Button android:id="+id/logout_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="color/colorPrimary" android:onClick="doLogout" android:text="string/logout" android:textColor="android:color/white" android:textSize="14sp" android:visibility="gone" /> <!-- Share button only visible after successful Sign In --> <Button android:id="+id/share_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="color/colorPrimary" android:onClick="onShareClick" android:text="string/share" android:textColor="android:color/white" android:textSize="14sp" android:visibility="gone" /> <!-- Open Profile Links buttons to open user profile into LinkedIn App only visible after successful Sign In --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="+id/open_my_profile_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_weight="1" android:background="color/colorPrimary" android:onClick="openMyProfile" android:layout_marginRight="5dp" android:text="string/open_my_profile" android:textColor="android:color/white" android:textSize="14sp" android:visibility="gone" /> <Button android:id="+id/open_others_profile_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="color/colorPrimary" android:onClick="openOthersProfile" android:text="string/open_others_profile" android:textColor="android:color/white" android:textSize="14sp" android:visibility="gone" /> </LinearLayout> </LinearLayout> </android.support.v4.widget.NestedScrollView> |
You can download LinkedIn official button image to place background to your Button. Download Button image from here.
4. Now open MainActivity.java and place the below code to it. In this class every feature is there that we discusses above like SignIn, Share, Open profile page and Logout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
package com.android_linkedin_integration_demo; import android.annotation.SuppressLint; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.Signature; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Base64; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.linkedin.platform.APIHelper; import com.linkedin.platform.DeepLinkHelper; import com.linkedin.platform.LISessionManager; import com.linkedin.platform.errors.LIApiError; import com.linkedin.platform.errors.LIAuthError; import com.linkedin.platform.errors.LIDeepLinkError; import com.linkedin.platform.listeners.ApiListener; import com.linkedin.platform.listeners.ApiResponse; import com.linkedin.platform.listeners.AuthListener; import com.linkedin.platform.listeners.DeepLinkListener; import com.linkedin.platform.utils.Scope; import com.squareup.picasso.Picasso; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private ImageView userImageView; private TextView userDetails; private Button signInButton, logoutButton, shareButton, openMyProfileButton, openOtherProfileButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } /** * init the views by finding the ID of all views */ private void initViews() { signInButton = findViewById(R.id.linkedin_login_button); logoutButton = findViewById(R.id.logout_button); userImageView = findViewById(R.id.user_profile_image_view); userDetails = findViewById(R.id.user_details_label); shareButton = findViewById(R.id.share_button); openMyProfileButton = findViewById(R.id.open_my_profile_button); openOtherProfileButton = findViewById(R.id.open_others_profile_button); } /** * method to get Hash key for current app package which will be used to add in LinkedIn Application settings * call this method once to get Key Hash */ private void getPackageHash() { try { @SuppressLint("PackageManagerGetSignatures") PackageInfo info = getPackageManager().getPackageInfo( "com.android_linkedin_integration_demo",//give your package name here PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d(TAG, "Hash : " + Base64.encodeToString(md.digest(), Base64.NO_WRAP));//Key hash is printing in Log } } catch (PackageManager.NameNotFoundException e) { Log.d(TAG, e.getMessage(), e); } catch (NoSuchAlgorithmException e) { Log.d(TAG, e.getMessage(), e); } } // Build the list of member permissions our LinkedIn session requires private static Scope buildScope() { //Check Scopes in Application Settings before passing here else you won't able to read that data return Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS, Scope.W_SHARE); } /** * on Sign In button do LinkedIn Authentication * * @param view */ public void signInWithLinkedIn(View view) { //First check if user is already authenticated or not and session is valid or not if (!LISessionManager.getInstance(this).getSession().isValid()) { //if not valid then start authentication LISessionManager.getInstance(getApplicationContext()).init(this, buildScope()//pass the build scope here , new AuthListener() { @Override public void onAuthSuccess() { // Authentication was successful. You can now do // other calls with the SDK. Toast.makeText(MainActivity.this, "Successfully authenticated with LinkedIn.", Toast.LENGTH_SHORT).show(); //on successful authentication fetch basic profile data of user fetchBasicProfileData(); } @Override public void onAuthError(LIAuthError error) { // Handle authentication errors Log.e(TAG, "Auth Error :" + error.toString()); Toast.makeText(MainActivity.this, "Failed to authenticate with LinkedIn. Please try again.", Toast.LENGTH_SHORT).show(); } }, true);//if TRUE then it will show dialog if // any device has no LinkedIn app installed to download app else won't show anything } else { Toast.makeText(this, "You are already authenticated.", Toast.LENGTH_SHORT).show(); //if user is already authenticated fetch basic profile data for user fetchBasicProfileData(); } } /** * method to fetch basic profile data */ private void fetchBasicProfileData() { //In URL pass whatever data from user you want for more values check below link //LINK : https://developer.linkedin.com/docs/fields/basic-profile String url = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,public-profile-url,picture-url,email-address,picture-urls::(original))"; APIHelper apiHelper = APIHelper.getInstance(getApplicationContext()); apiHelper.getRequest(this, url, new ApiListener() { @Override public void onApiSuccess(ApiResponse apiResponse) { // Success! Log.d(TAG, "API Res : " + apiResponse.getResponseDataAsString() + "\n" + apiResponse.getResponseDataAsJson().toString()); Toast.makeText(MainActivity.this, "Successfully fetched LinkedIn profile data.", Toast.LENGTH_SHORT).show(); //update UI on successful data fetched updateUI(apiResponse); } @Override public void onApiError(LIApiError liApiError) { // Error making GET request! Log.e(TAG, "Fetch profile Error :" + liApiError.getLocalizedMessage()); Toast.makeText(MainActivity.this, "Failed to fetch basic profile data. Please try again.", Toast.LENGTH_SHORT).show(); } }); } /** * method to update UI * * @param apiResponse after fetching basic profile data */ private void updateUI(ApiResponse apiResponse) { try { if (apiResponse != null) { JSONObject jsonObject = apiResponse.getResponseDataAsJson(); //display user basic details userDetails.setText("Name : " + jsonObject.getString("firstName") + " " + jsonObject.getString("lastName") + "\nHeadline : " + jsonObject.getString("headline") + "\nEmail Id : " + jsonObject.getString("emailAddress")); //use the below string value to display small profile picture String smallPicture = jsonObject.getString("pictureUrl"); //use the below json parsing for different profile pictures and big size images JSONObject pictureURLObject = jsonObject.getJSONObject("pictureUrls"); if (pictureURLObject.getInt("_total") > 0) { //get array of picture urls JSONArray profilePictureURLArray = pictureURLObject.getJSONArray("values"); if (profilePictureURLArray != null && profilePictureURLArray.length() > 0) { // get 1st image link and display using picasso Picasso.with(this).load(profilePictureURLArray.getString(0)) .placeholder(R.mipmap.ic_launcher_round) .error(R.mipmap.ic_launcher_round) .into(userImageView); } } else { // if no big image is available then display small image using picasso Picasso.with(this).load(smallPicture) .placeholder(R.mipmap.ic_launcher_round) .error(R.mipmap.ic_launcher_round) .into(userImageView); } //show hide views signInButton.setVisibility(View.GONE); logoutButton.setVisibility(View.VISIBLE); userDetails.setVisibility(View.VISIBLE); userImageView.setVisibility(View.VISIBLE); shareButton.setVisibility(View.VISIBLE); openMyProfileButton.setVisibility(View.VISIBLE); openOtherProfileButton.setVisibility(View.VISIBLE); } } catch (Exception e) { e.printStackTrace(); } } public void doLogout(View view) { //clear session on logout click LISessionManager.getInstance(this).clearSession(); //show hide views signInButton.setVisibility(View.VISIBLE); logoutButton.setVisibility(View.GONE); userDetails.setVisibility(View.GONE); userImageView.setVisibility(View.GONE); shareButton.setVisibility(View.GONE); openMyProfileButton.setVisibility(View.GONE); openOtherProfileButton.setVisibility(View.GONE); //show toast Toast.makeText(this, "Logout successfully.", Toast.LENGTH_SHORT).show(); } /** * method call on share click * * @param view */ public void onShareClick(View view) { //LINK : https://developer.linkedin.com/docs/share-on-linkedin String url = "https://api.linkedin.com/v1/people/~/shares"; JSONObject body = null; try { //prepare json object for sharing body = new JSONObject("{" + "\"comment\": \"Test Comment\"," + //A comment by the member to associated with the share. // If none of the above content parameters are provided, the comment must contain a URL to the content you want to share. If the comment contains multiple URLs, only the first one will be analyzed for content to share. "\"visibility\": { \"code\": \"anyone\" }," +//A collection of visibility information about the share. //One of the following values: // anyone: Share will be visible to all members. // connections-only: Share will only be visible to connections of the member performing the share. // This field is required in all sharing calls. "\"content\": { " + "\"title\": \"Test share\"," +//The title of the content being shared. "\"description\": \"Testing the mobile SDK share feature!\"," +//The description of the content being shared. "\"submitted-url\": \"https://www.androhub.com/\"," +//A fully qualified URL for the content being shared. "\"submitted-image-url\": \"https://androhub.com/wp-content/uploads/2016/05/andro_new.png\"" +//A fully qualified URL to a thumbnail image to accompany the shared content. The image should be at least 80 x 150px for best results. "}" + "}"); } catch (JSONException e) { e.printStackTrace(); } APIHelper apiHelper = APIHelper.getInstance(getApplicationContext()); apiHelper.postRequest(this, url, body, new ApiListener() { @Override public void onApiSuccess(ApiResponse apiResponse) { // Success! Log.d(TAG, "Share response : " + apiResponse.toString()); Toast.makeText(MainActivity.this, "Shared successfully.", Toast.LENGTH_SHORT).show(); } @Override public void onApiError(LIApiError liApiError) { // Error making POST request! Log.e(TAG, "Share error : " + liApiError.getLocalizedMessage()); Toast.makeText(MainActivity.this, "Failed to share. Please try again.", Toast.LENGTH_SHORT).show(); } }); } /** * method to open current profile using LinkedIn DeepLink helper * * @param view */ public void openMyProfile(View view) { DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance(); // Open the current user's profile deepLinkHelper.openCurrentProfile(this, new DeepLinkListener() { @Override public void onDeepLinkSuccess() { // Successfully sent user to LinkedIn app Toast.makeText(MainActivity.this, "Current profile opened successfully.", Toast.LENGTH_SHORT).show(); } @Override public void onDeepLinkError(LIDeepLinkError error) { // Error sending user to LinkedIn app Log.e(TAG, "Current profile open error : " + error.toString()); Toast.makeText(MainActivity.this, "Failed to open current profile.", Toast.LENGTH_SHORT).show(); } }); } /** * method to open others profile by passing their IDs * NOTE : Storing member ID values on your own server allows your application to direct users to the official LinkedIn profiles * for any other users that have authorized your mobile application. * * @param view */ public void openOthersProfile(View view) { //A sample member ID value to open for this first you have to get user id final String targetID = "any_id"; DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance(); // Open the target LinkedIn member's profile deepLinkHelper.openOtherProfile(this, targetID, new DeepLinkListener() { @Override public void onDeepLinkSuccess() { // Successfully sent user to LinkedIn app Toast.makeText(MainActivity.this, "Other profile opened successfully.", Toast.LENGTH_SHORT).show(); } @Override public void onDeepLinkError(LIDeepLinkError error) { // Error sending user to LinkedIn app Log.e(TAG, "Other profile open error : " + error.toString()); Toast.makeText(MainActivity.this, "Failed to open other profile.", Toast.LENGTH_SHORT).show(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Add this line to your existing onActivityResult() method LISessionManager.getInstance(getApplicationContext()).onActivityResult(this, requestCode, resultCode, data); // Add this line to your existing onActivityResult() method DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance(); deepLinkHelper.onActivityResult(this, requestCode, resultCode, data); } /** * cancel API calls */ private void cancelAPIRequest(){ APIHelper apiHelper = APIHelper.getInstance(getApplicationContext()); apiHelper.cancelCalls(this); } } |
5. Finally all done , now you can also add LinkedIn integration into your app.
Thanks.
Subscribe to us and get the latest news.
12 Comments
Puneet
Wednesday, September 12th, 2018Really Helpful
Nikita Prajapati
Wednesday, January 23rd, 2019Hello,
I got this error while integrating LinkedIn
please help
Dr. Droid
Wednesday, January 23rd, 2019Hi Nikita
What error you are getting can you share your error or logs.
Thanks
Khurram
Thursday, January 24th, 2019I implemented this in my project and it was working fine, But suddenly few days back I am unable to sign in and getting access token equal to null. so whenever I fetch basic data onApiError function called.
Dr. Droid
Friday, January 25th, 2019Hi Khurram,
For this you can again request login to user.
Thanks
Reddy
Friday, January 25th, 2019is linked in sdk stopped ? not able to get logged user information
Dr. Droid
Friday, January 25th, 2019Hi Reddy,
Yes it supports I think. If not then upgrade the SDK and try.
Thanks
Yıldırım TAM
Thursday, February 28th, 2019I get “access toke is not to set” error” and I can not solve 🙁
Dr. Droid
Thursday, February 28th, 2019Hi Yildirim,
Can you confirm that you have followed correct step to create app over LinkedIn and implementing it?
Thanks
Rahma
Thursday, February 28th, 2019Hello ,Sir ,i use th code but i cant get the email address ,and i have error ,Accesstoken 0
Any help plz
Dr. Droid
Friday, March 1st, 2019Hi Rahma,
I saw this error too and its due to that LinkedIn is returning NULL access token during SignIn.
LinkedIn moved the APIs to V2 so I will update you once the issue is fixed.
Thanks
Krutika chotara
Wednesday, March 27th, 2019Hello i am getting
Auth Error :{
“errorCode”: “UNKNOWN_ERROR”,
“errorMessage”: “The passed in mobile identifier is invalid \”com.android_linkedin_integration_demo\””
}
Please help me to solve this