0

I was able to integrate Google SignIn and Facebook SignIn effortlessly due to their transparent docs. However, I'm having trouble with linked in. All I want to do is get the userId from a successful LinkedIn login attempt.

As shown in the docs: https://developer.linkedin.com/docs/android-sdk

I successfully generated a debug hash key. But now, I'm just having trouble figuring out how to configure linkedIn Signin in my project.

  1. I have Facebook & Google's login dependencies in my app, but What dependencies do I need in my build.gradle for LinkedIn Signin?

    dependencies {
    
      implementation 'com.facebook.android:facebook-login:[4,5)' //Facebook
      implementation 'com.google.android.gms:play-services-auth:15.0.1' //Google
      implementation <insert_linkedin_signInDependency_Here>
    
    }
    
  2. I am able to successfully get userId from Facebook and Google setOnClickListener methods. But am unsure how to get userId when linkedIn button is clicked.

    //Getting Facebook userId
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            final String userID = accessToken.getUserId();
      }
    });
    
grantespo
  • 2,233
  • 2
  • 24
  • 62
  • 1
    Hi. Can you try as specified here:https://github.com/sambhaji213/LinkedIn-Login/blob/6f2853237a7e86f77dbc405af60a3a1c89f941cb/app/build.gradle along with https://stackoverflow.com/a/40386638/1004631 ? – robot_alien Dec 09 '18 at 17:39
  • 1
    @robot_alien thanks so much, linkedin's Android sdk hasn't been updated in 3 years so I guess everyhing has to be done "old school" lol. – grantespo Dec 09 '18 at 17:42
  • 1
    Np, let me know if this works. Can help me later if I really need to embed LinkedIn as a way to enable users to login into my app... :) – robot_alien Dec 09 '18 at 17:45
  • possible duplicate https://stackoverflow.com/questions/54513859/how-to-implement-login-with-linkedin-with-oauth-2-0-in-android – Sumudu Sahan Weerasuriya Jul 31 '20 at 10:48

2 Answers2

0

I created a small library to implement LinkedIn Authentication via OAuth2

Library - https://github.com/Sumudu-Sahan/LinkedInManager

Steps

  1. Add the below maven dependency to your project level build.gradle file

    allprojects { repositories { ... maven { url 'https://jitpack.io' } } }

  2. add the below maven dependency to your app level build.gradle file

    dependencies { implementation 'com.github.Sumudu-Sahan:LinkedInManager:1.00.02' }

  3. Inherit your activity, fragment from LinkedInManagerResponse

    public class MainActivity extends AppCompatActivity implements LinkedInManagerResponse

  4. Initiate the LinkedInRequestManager object instance for login process

    LinkedInRequestManager linkedInRequestManager = new LinkedInRequestManager(Activity, LinkedInManagerResponse, "CLIENT ID", "CLIENT SECRET", "REDIRECTION URL");

  5. Start Authenticate with below statement

linkedInRequestManager.showAuthenticateView(LinkedInRequestManager.MODE_BOTH_OPTIONS);

Available Modes

LinkedInRequestManager.MODE_EMAIL_ADDRESS_ONLY
LinkedInRequestManager.MODE_LITE_PROFILE_ONLY
LinkedInRequestManager.MODE_BOTH_OPTIONS
0

If you're willing to use 3rd party libs, I've created a lightweight "unofficial" SDK for Android, you can use it from this GitHub repo, disclaimer: I wrote the lib when faced with the same use case.

So using this library, you will need to do two things to get the user id you need from LinkedIn

  1. First, use LinkedInBuilder class and provide it with your clientId, clientSecret, redirectUrl and LINKEDIN_REQUEST_Id and use it to authenticate the user, currently we support two types of LinkedInBuilder: LinkedInFromActivityBuilder and LinkedInFromFragmentBuilder. Use the one you need based on the call site, so if you're authenticating from an Activity you need the first, and if authenticating from a Fragment use the second. You'll probably end up with something similar to the following:
LinkedInFromFragmentBuilder.getInstance(LoginFragment.this)
                        .setClientID(clientID)
                        .setClientSecret(clientSecret)
                        .setRedirectURI(redirectUrl)
                        .authenticate(LINKEDIN_REQUEST);
  1. You'll need to override onActivityResult, much as what you're doing with Facebook and Google, you will need also to check if the request coming back is same as LINKEDIN_REQUEST you already provided. If so, and authenticating the user succeeds, you can obtain a LinkedInUser model, which contains the userId you're looking for. Your onActivityResult will be something like the following:
  @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == LINKEDIN_REQUEST && data != null) {
            if (resultCode == RESULT_OK) {

                //Successfully signed in and retrieved data
                LinkedInUser user = data.getParcelableExtra("social_login");
                String userId = user.getId();
                // do whatever you need with the id.
            } else {
                //print the error
                String errMsg = data.getStringExtra("err_message");
                }
            }
        }

That should be making you good to go. :)

Boda
  • 329
  • 2
  • 11