Showing posts with label Gmail from linkedin. Show all posts
Showing posts with label Gmail from linkedin. Show all posts

Thursday, December 08, 2016

Linkedin Integration, Login with Linkedin

Linkedin Integration - To provide the one click signup and signin process in the application, Android developers provide the login with social sites facilities like- facebook, twitter, linkedin, g+ etc. This tutorial guide you for Linkedin one click signup and signin process.



Steps to use recycle view in your project.
  1. Crate a project in android studio.
  2. First download linkedin sdk(Download SDK)  and import as a module in your project.

  3. Open build.gradle(app) and add some dependency

    //______add linkedin-sdk as a dependency
    compile project(':linkedin-sdk')
    
    //______add picaso dependency for image loding
    compile 'com.squareup.picasso:picasso:2.5.2'
    
      
  4.  Click on "Sync Now"
  5. Open your main_activity.xml and update it-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:gravity="center"
        android:orientation="vertical"
        tools:context="com.pankaj.linkedindemo.MainActivity">
    
        <!--to show user image-->
        <ImageView
            android:id="@+id/ivUser"
            android:layout_width="180dp"
            android:layout_height="180dp"
            android:layout_margin="5dp"
            android:src="@drawable/ic_launcher" />
    
        <!--to show user name-->
        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="Point of Android" />
    
        <!--to show user email-->
        <TextView
            android:id="@+id/tvMail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="e@mail.com" />
    
        <!--for login with linkedin-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="login"
            android:text="Login with linkedin" />
    
        <!--for generate keyhash-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="generateHashkey"
            android:text="Genrate KeyHash" />
    
        <!--to show package name-->
        <TextView
            android:id="@+id/tvPackage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="package" />
    
        <!--to show keyhash-->
        <TextView
            android:id="@+id/tvKeyHash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="Keyhash" />
    </LinearLayout>
    
    
    
     
  6. Open your MainActivity.JAVA class and update it -
    package com.pankaj.linkedindemo;
    
    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.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.linkedin.platform.APIHelper;
    import com.linkedin.platform.LISessionManager;
    import com.linkedin.platform.errors.LIApiError;
    import com.linkedin.platform.errors.LIAuthError;
    import com.linkedin.platform.listeners.ApiListener;
    import com.linkedin.platform.listeners.ApiResponse;
    import com.linkedin.platform.listeners.AuthListener;
    import com.linkedin.platform.utils.Scope;
    import com.squareup.picasso.Picasso;
    import org.json.JSONObject;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    public class MainActivity extends AppCompatActivity {
    
        //__________create objects
        private TextView tvName, tvMail, tvPackage, tvKeyHash;
        private ImageView ivUser;
    
        private final String TAG = MainActivity.class.getSimpleName();
        private final String PACKAGE = "com.pankaj.linkedindemo";
        private final String host = "api.linkedin.com";
        private final String topCardUrl = "https://" + host + "/v1/people/~:(email-address,formatted-name,phone-numbers,public-profile-url,picture-url,picture-urls::(original))";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //___________initialization
            tvName = (TextView) findViewById(R.id.tvName);
            tvMail = (TextView) findViewById(R.id.tvMail);
            tvPackage = (TextView) findViewById(R.id.tvPackage);
            tvKeyHash = (TextView) findViewById(R.id.tvKeyHash);
            ivUser = (ImageView) findViewById(R.id.ivUser);
    
        }
    
        //____________methode will be call when click on login with linkedin
        public void login(View view) {
    
            //____________authentication request
            LISessionManager.getInstance(getApplicationContext()).init(this, buildScope(), new AuthListener() {
    
                @Override
                public void onAuthSuccess() {
                }
    
                @Override
                public void onAuthError(LIAuthError error) {
                    Toast.makeText(getApplicationContext(), "failed " + error.toString(), Toast.LENGTH_LONG).show();
                }
            }, true);
        }
    
        //_________create scope to get permission for access data from linkedin.
        private static Scope buildScope() {
            return Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS);
        }
    
        //___________After complete authentication start new HomePage Activity
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            LISessionManager.getInstance(getApplicationContext()).onActivityResult(this, requestCode, resultCode, data);
            getUserData();
        }
    
        //___________get data from linked in server
        private void getUserData() {
    
            //_______add request to get data
            APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
            apiHelper.getRequest(this, topCardUrl, new ApiListener() {
    
                @Override
                public void onApiSuccess(ApiResponse result) {
    
                    //_________set captured data on screen
                    setUserProfile(result.getResponseDataAsJson());
                }
    
                @Override
                public void onApiError(LIApiError error) {
                    error.printStackTrace();
                }
            });
        }
    
        //___________set data to the screen
        public void setUserProfile(JSONObject response) {
    
            try {
    
                Log.e("linkedin response", response.toString());
    
                //__set email address
                tvMail.setText(response.get("emailAddress").toString());
    
                //__set Name
                tvName.setText(response.get("formattedName").toString());
    
                //parce emage address
                String imageUrl = (String) response.getJSONObject("pictureUrls").getJSONArray("values").get(0);
    
                //_________load profile image image
                Picasso.with(this).load(imageUrl).into(ivUser);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //______________genrate hashkey
        public void generateHashkey(View view) {
    
            try {
    
                PackageInfo info = getPackageManager().getPackageInfo(PACKAGE, PackageManager.GET_SIGNATURES);
    
                for (Signature signature : info.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    md.update(signature.toByteArray());
    
                    //___________set package name
                    tvPackage.setText(PACKAGE);
    
                    //___________set keyhash
                    tvKeyHash.setText(Base64.encodeToString(md.digest(), Base64.NO_WRAP));
    
                    //___________add package name and key hash into log
                    Log.e(TAG, "_____________________________________________");
                    Log.e("Package", PACKAGE);
                    Log.e("KeyHash", tvKeyHash.getText().toString());
                    Log.e(TAG, "_____________________________________________");
                    Toast.makeText(this, "Check your logcate for keyhash", Toast.LENGTH_SHORT).show();
    
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.d(TAG, e.getMessage(), e);
            } catch (NoSuchAlgorithmException e) {
                Log.d(TAG, e.getMessage(), e);
            }
        }
    }
    
    

     
  7. Now all the development steps for linkedin integration has completed, Please run the application and see the screen of device.




  8. Now add an application on linkedin developer console(Linkedin Developer Console).

    click on Create application
  9. fill all the information required to create the app and submit it.

  10. Look at the "Default Application Permissions". check the required permission box.

  11. Click on update.
  12. Select mobile from the left menu.

  13. Look on the "Android Settings" add package name and package Key Hash

  14. For package name and key has check your logcate.


     
  15. Click on update.

  16. Now go to the mobile application and click on "Login with Linkedin" and you will see the permission screen, then click on "OK".
  17. You can see the logedin user data(name,email and  image)  on you screen
  18. Good bye, Thanks to read this blog.