Wednesday, 23 August 2017

LinkedIn Integration - Andorid ~ programming info ~ gniithelp

Introduction

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.

Overview of SDK

  • The mobile SDK for Android increases your app's time to market by providing out-of-box support for LinkedIn natively inside your Android applications.
  • The Mobile SDK for Android requires the official LinkedIn Android application is also installed to support the SDK's capabilities.
  • The minimum supported version is Android 4.4.2 (API 19).

 

SDK Features

  • Single sign-on (SSO) authentication, in conjunction with the LinkedIn mobile app.
  • A convenient wrapper for making authenticated calls to LinkedIn's REST APIs.
  • "Deep linking" to additional member data in the LinkedIn mobile app.
  • Sample application that demonstrate best-practice implementations of all of the SDK's features.

Please find steps below for LinkedIn integration in Android.

 

    Step - 1 Create Application  
    • To Integrate LinkedIn in your mobile application, you need to create a new application using LinkedIn Developer’s Account.
    • Create application from LinkedIn developer account.
      https://www.linkedin.com/developer/apps



      Step - 2 Set the Application Permission
      • Now, you need to set  the Default Application Permissions.
        And to do that, you have to select check box “r_basicprofile” and “r_emailaddress” and click on the “update” button to set the permission.



        Step - 3 Download Mobile LinkedIn SDK

        Go to https://developer.linkedin.com/docs/android-sdk  and download a Mobile SDK for Android.


        Step - 4 Generate Hash Key
        • We need to generate a hash key. This generated Hash key will integrate your app with Linkedin account.
        • Go to https://www.linkedin.com/developer/apps  
        • Select your application name and click the Mobile tab. 
        • Add the package name and generated hash key in your LinkedIn Application.
        • This hash key will authenticate your mobile application. 



        Login

        private static final String topCardUrl = "https://" + host + "/v1/people/~:(first-name,last-name,email-address,formatted-name,phone-numbers,public-profile-url,picture-url,picture-urls::(original))"
        private static Scope buildScope() {
            return Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS, Scope.W_SHARE);
        }
        public void loginLinkedin() {
        LISessionManager.getInstance(getApplicationContext()).init(this,
           buildScope(), new AuthListener() {
                 @Override 
                 public void onAuthSuccess() {
        
                   APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
                   apiHelper.getRequest(MainActivity.this, topCardUrl, new ApiListener() {
                        @Override                         
                        public void onApiSuccess(ApiResponse s) {
                                   
                         Log.e(TAG, "Profile json" + s.getResponseDataAsJson());
                         Log.e(TAG, "Profile String" + s.getResponseDataAsString());
        
                           try {
                             Log.e(TAG, "Profile emailAddress" + s.getResponseDataAsJson().get("emailAddress").toString());
                             Log.e(TAG, "Profile formattedName" + s.getResponseDataAsJson().get("formattedName").toString());
        
                             txtFirstName.setText(s.getResponseDataAsJson().get("emailAddress").toString());
                             txtLastName.setText(s.getResponseDataAsJson().get("formattedName").toString()); 
                             Picasso.with(MainActivity.this).load(s.getResponseDataAsJson().getString("pictureUrl"))
                                    .into(imgProfilePic);
        
                                }catch (Exception e){
        
                                }
        
                      }
        
                        @Override 
                        public void onApiError(LIApiError error) {
                                    //((TextView) findViewById(R.id.response)).setText(error.toString());                            Toast.makeText(getApplicationContext(), "Profile failed " + error.toString(),
                                            Toast.LENGTH_LONG).show();
                                }
                            });
        
                        }
        
                 @Override                 
                 public void onAuthError(LIAuthError error) {
        
                 Toast.makeText(getApplicationContext(), "failed " + error.toString(),
                                    Toast.LENGTH_LONG).show();
                        }
                    }, true);

        Logout

        LISessionManager.getInstance(getApplicationContext()).clearSession();

        Check Login

        private boolean isLogin(){
            LISessionManager sessionManager = LISessionManager.getInstance(getApplicationContext());
            LISession session = sessionManager.getSession();
            boolean accessTokenValid = session.isValid();
            return accessTokenValid;
        }
        Share Message 
        private static final String shareUrl = "https://" + host + "/v1/people/~/shares";
         
        
        public void shareMessage() {
            APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
            apiHelper.postRequest(MainActivity.this, shareUrl, buildShareMessage("Hello World", "Hello Title", "Hello Descriptions", "http://ankitthakkar90.blogspot.in/", "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsjG5bDoeWImoTtulgC2uLgf8RIwHCNvw3zmxyMWU4UCXLptGhJTeg3UvcYulwB7CfFqusllntSY_bdVmK5KihS9xgooNkFdcnvYyb-FPruCL0ppE7fCuUM_GqYijD9Q9WAaxhLaJTkpgk/s320/10333099_1408666882743423_2079696723_n.png"), new ApiListener() {
                @Override 
                public void onApiSuccess(ApiResponse apiResponse) {
                    // ((TextView) findViewById(R.id.response)).setText(apiResponse.toString());            Toast.makeText(getApplicationContext(), "Share success:  " + apiResponse.toString(),
                            Toast.LENGTH_LONG).show();
                    Log.e(TAG, "share success" + apiResponse.toString());
                }
        
                @Override         
                public void onApiError(LIApiError error) {
                    //   ((TextView) findViewById(R.id.response)).setText(error.toString());            Toast.makeText(getApplicationContext(), "Share failed " + error.toString(),
                            Toast.LENGTH_LONG).show();
                }
            });
        } 
        
        
        
        public String buildShareMessage(String comment,String title,String descriptions,String linkUrl,String imageUrl  ){
            String shareJsonText = "{ \n" +
                    "   \"comment\":\"" + comment + "\"," +
                    "   \"visibility\":{ " +
                    "      \"code\":\"anyone\"" +
                    "   }," +
                    "   \"content\":{ " +
                    "      \"title\":\""+title+"\"," +
                    "      \"description\":\""+descriptions+"\"," +
                    "      \"submitted-url\":\""+linkUrl+"\"," +
                    "      \"submitted-image-url\":\""+imageUrl+"\"" +
                    "   }" +
                    "}";
            return shareJsonText;
        }
        Open Current User Profile
        public void openUserProfile(){
            DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance();
            deepLinkHelper.openCurrentProfile(MainActivity.this, new DeepLinkListener() {
                @Override         
                public void onDeepLinkSuccess() {
                    Log.e(TAG, "openUserProfile success");
                }
        
                @Override 
                public void onDeepLinkError(LIDeepLinkError error) {
                    Log.e(TAG, "openUserProfile error" + error.toString());
                }
            });
        }

        Best Practices

        Posting on member's behalf
        •  Members assume that they will have control on what content is posted and shared on their behalf. You should assure users that you will not post or send mail on their behalf without their consent, and give them the option to edit content before it is posted or not share content if they choose.

        Permission Request
        • You should educate users on which permissions you are requesting and how this data will be used. LinkedIn does not support incremental permission request, so all permissions must be granted during the authorization step.  
        •  Requesting too many permissions may cause users not to authorize your application, so you should only ask for the permissions that you need.
        Authentication
        • Whenever possible, remind the user that they are logged into your application by displaying their name, portrait, and/or account settings.
        • You should also avoid multiple log in prompts. 
        • Cache the user's access token after they grant your application and do not bring the user through the authentication flow again unless they log out or the access token expires or is otherwise invalid.
        • You should allow the user to log out, and when they do log out you should destroy the access token you had been granted.

         

        Cancelling in-progress requests

        During your application's workflow, you may wish to cancel any in-progress API requests.  This is done by calling APIHelper.cancelCalls() method.

        Using ProGaurd with your application 

        If you intend to use ProGuard on the release build of your mobile application, you will need to add the following lines to your project's proguard-project.txt file to preserve information required for the SDK to function properly:

        proguard configuration

        -keep class com.linkedin.** { *; }
        -keepattributes Signature

        Mobile vs. server-side access tokens            

        It is important to note that access tokens that are acquired via the Mobile SDK are only useable with the Mobile SDK, and cannot be used to make server-side REST API calls.

        Similarly, access tokens that you already have stored from your users that authenticated using a server-side REST API call will not work with the Mobile SDK.

        Partnership Program

        • All other APIs (e.g. Connections, Groups, People Search, Invitation, Job Search, etc.) will require developers to become a member of one of our partnership programs.  
        • Partnering with LinkedIn provides you with additional API functionality & data access, increased call limits & dedicated support.
        • Applications are only accepted when we feel that they're providing value to members, developers and LinkedIn.
        https://developer.linkedin.com/partner-programs

        References

        https://developer.linkedin.com/docs/android-sdk
        https://developer.linkedin.com/docs/android-sdk-auth
        https://developer.linkedin.com/downloads#androidsdk
        https://www.numetriclabz.com/android-linkedin-integration-login-tutorial/
        https://developer.linkedin.com/docs/oauth2
        https://developer.linkedin.com/partner-programs/apply
        https://developer.linkedin.com/support/developer-program-transition
        http://www.solutionanalysts.com/blog/step-by-step-developers-guide-to-integrate-linkedin-with-an-android-application/ 

        You can Download source code of this example from Github.

        No comments:

        Post a Comment