Sunday, 19 May 2013

Linkedin Integration in android | Android Linkedin Api | Post on Linkedin | Android Linkedin Integration Tutorial|LinkedIn Integration in Android App



Hello Friends,

I am going to share the steps of LinkedIn Integration in android.

Steps for LinkedIn Integration :

     1. Go to developer site(https://www.linkedin.com/secure/developer) and create
         an APP.
         
linkedin key
linkedin
   


  2. Getting API key(Consumer Key)  and Secret key:
   
linkedin



  3. Add following code in Your app and don't forgot to replace the key
      from the config.java file.
     





1. LinkedInSampleActivity

package com.example.linkedin;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.EnumSet;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.linkedin.R;
import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.enumeration.ProfileField;
import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthService;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInRequestToken;
import com.google.code.linkedinapi.schema.Person;
import com.mukesh.linkedin.LinkedinDialog.OnVerifyListener;
import com.squareup.picasso.Picasso;

/**
 * @author Mukesh Kumar Yadav
 */
public class LinkedInSampleActivity extends Activity {
 Button login;
 Button share;
 EditText et;
 TextView name,profile;
 ImageView photo;
 public static final String OAUTH_CALLBACK_HOST = "litestcalback";

 final LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory
            .getInstance().createLinkedInOAuthService(
                    Config.LINKEDIN_CONSUMER_KEY,Config.LINKEDIN_CONSUMER_SECRET);
 final LinkedInApiClientFactory factory = LinkedInApiClientFactory
   .newInstance(Config.LINKEDIN_CONSUMER_KEY,
     Config.LINKEDIN_CONSUMER_SECRET);
 LinkedInRequestToken liToken;
 LinkedInApiClient client;
 LinkedInAccessToken accessToken = null;

 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
 @SuppressLint("NewApi")
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  if( Build.VERSION.SDK_INT >= 9){
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy); 
  }
  share = (Button) findViewById(R.id.share);
  name = (TextView) findViewById(R.id.name);
  profile = (TextView) findViewById(R.id.profile);
  et = (EditText) findViewById(R.id.et_share);
  login = (Button) findViewById(R.id.login);
  photo = (ImageView) findViewById(R.id.photo);

  login.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    linkedInLogin();
   }
  });

  // share on linkedin
  share.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
    String share = et.getText().toString();
    if (null != share && !share.equalsIgnoreCase("")) {
     OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Config.LINKEDIN_CONSUMER_KEY, Config.LINKEDIN_CONSUMER_SECRET);
        consumer.setTokenWithSecret(accessToken.getToken(), accessToken.getTokenSecret());
     DefaultHttpClient httpclient = new DefaultHttpClient();
     HttpPost post = new HttpPost("https://api.linkedin.com/v1/people/~/shares");
     try {
      consumer.sign(post);
     } catch (OAuthMessageSignerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (OAuthExpectationFailedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (OAuthCommunicationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } // here need the consumer for sign in for post the share
     post.setHeader("content-type", "text/XML");
     String myEntity = "<share><comment>"+ share +"</comment><visibility><code>anyone</code></visibility></share>";
     try {
      post.setEntity(new StringEntity(myEntity));
      org.apache.http.HttpResponse response = httpclient.execute(post);
      Toast.makeText(LinkedInSampleActivity.this,
        "Shared sucessfully", Toast.LENGTH_SHORT).show();
     } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }else {
     Toast.makeText(LinkedInSampleActivity.this,
       "Please enter the text to share",
       Toast.LENGTH_SHORT).show();
    }
    
    /*String share = et.getText().toString();
    if (null != share && !share.equalsIgnoreCase("")) {
     client = factory.createLinkedInApiClient(accessToken);
     client.postNetworkUpdate(share);
     et.setText("");
     Toast.makeText(LinkedInSampleActivity.this,
       "Shared sucessfully", Toast.LENGTH_SHORT).show();
    } else {
     Toast.makeText(LinkedInSampleActivity.this,
       "Please enter the text to share",
       Toast.LENGTH_SHORT).show();
    }*/
   }
  });
 }

 private void linkedInLogin() {
  ProgressDialog progressDialog = new ProgressDialog(
    LinkedInSampleActivity.this);

  LinkedinDialog d = new LinkedinDialog(LinkedInSampleActivity.this,
    progressDialog);
  d.show();

  // set call back listener to get oauth_verifier value
  d.setVerifierListener(new OnVerifyListener() {
   @SuppressLint("NewApi")
   public void onVerify(String verifier) {
    try {
     Log.i("LinkedinSample", "verifier: " + verifier);

     accessToken = LinkedinDialog.oAuthService
       .getOAuthAccessToken(LinkedinDialog.liToken,
         verifier);
     LinkedinDialog.factory.createLinkedInApiClient(accessToken);
     client = factory.createLinkedInApiClient(accessToken);
     // client.postNetworkUpdate("Testing by Mukesh!!! LinkedIn wall post from Android app");
     Log.i("LinkedinSample",
       "ln_access_token: " + accessToken.getToken());
     Log.i("LinkedinSample",
       "ln_access_token: " + accessToken.getTokenSecret());
     //Person p = client.getProfileForCurrentUser();
     Person p =  client.getProfileForCurrentUser(EnumSet.of(
       ProfileField.ID, ProfileField.FIRST_NAME,
       ProfileField.PHONE_NUMBERS, ProfileField.LAST_NAME,
       ProfileField.HEADLINE, ProfileField.INDUSTRY,
       ProfileField.PICTURE_URL, ProfileField.DATE_OF_BIRTH,
       ProfileField.LOCATION_NAME, ProfileField.MAIN_ADDRESS,
        ProfileField.LOCATION_COUNTRY));
     Log.e("create access token secret", client.getAccessToken()
       .getTokenSecret());

     if(p!=null) {
      name.setText("Welcome " + p.getFirstName() + " "
        + p.getLastName());
      name.setVisibility(0);
      profile.setText("Profile:"+p.getHeadline());
      profile.setVisibility(0);
      String id = p.getId();
      String url = p.getPictureUrl();
      if(url != null && !url.isEmpty()) {
       Picasso.with(LinkedInSampleActivity.this).load(url).into(photo);
       photo.setVisibility(0);
      }
      login.setVisibility(4);
      share.setVisibility(0);
      et.setVisibility(0);
     }

    } catch (Exception e) {
     Log.i("LinkedinSample", "error to get verifier");
     e.printStackTrace();
    }
   }
  });

  // set progress dialog
  progressDialog.setMessage("Loading...");
  progressDialog.setCancelable(true);
  progressDialog.show();
 }
}

2.LinkedinDialog:
package com.example.linkedin;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Picture;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebView.PictureListener;
import android.webkit.WebViewClient;

import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthService;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInRequestToken;

/**
 * Linkedin dialog
 * 
 * @author Mukesh Kumar Yadav
 */
public class LinkedinDialog extends Dialog {
 private ProgressDialog progressDialog = null;

 public static LinkedInApiClientFactory factory;
 public static LinkedInOAuthService oAuthService;
 public static LinkedInRequestToken liToken;

 /**
  * Construct a new LinkedIn dialog
  * 
  * @param context
  *            activity {@link Context}
  * @param progressDialog
  *            {@link ProgressDialog}
  */
 public LinkedinDialog(Context context, ProgressDialog progressDialog) {
  super(context);
  this.progressDialog = progressDialog;
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  requestWindowFeature(Window.FEATURE_NO_TITLE);// must call before super.
  super.onCreate(savedInstanceState);
  setContentView(R.layout.ln_dialog);

  setWebView();
 }

 /**
  * set webview.
  */
 private void setWebView() {
  LinkedinDialog.oAuthService = LinkedInOAuthServiceFactory.getInstance()
    .createLinkedInOAuthService(Config.LINKEDIN_CONSUMER_KEY,
      Config.LINKEDIN_CONSUMER_SECRET);
  LinkedinDialog.factory = LinkedInApiClientFactory.newInstance(
    Config.LINKEDIN_CONSUMER_KEY, Config.LINKEDIN_CONSUMER_SECRET);

  LinkedinDialog.liToken = LinkedinDialog.oAuthService
    .getOAuthRequestToken(Config.OAUTH_CALLBACK_URL);

  WebView mWebView = (WebView) findViewById(R.id.webkitWebView1);
  mWebView.getSettings().setJavaScriptEnabled(true);

  Log.i("LinkedinSample", LinkedinDialog.liToken.getAuthorizationUrl());
  mWebView.loadUrl(LinkedinDialog.liToken.getAuthorizationUrl());
  mWebView.setWebViewClient(new HelloWebViewClient());

  mWebView.setPictureListener(new PictureListener() {
   @Override
   public void onNewPicture(WebView view, Picture picture) {
    if (progressDialog != null && progressDialog.isShowing()) {
     progressDialog.dismiss();
    }

   }
  });

 }

 /**
  * webview client for internal url loading
  */
 class HelloWebViewClient extends WebViewClient {
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   if (url.contains(Config.OAUTH_CALLBACK_URL)) {
    Uri uri = Uri.parse(url);
    String verifier = uri.getQueryParameter("oauth_verifier");

    cancel();

    for (OnVerifyListener d : listeners) {
     // call listener method
     d.onVerify(verifier);
    }
   } else if (url
     .contains("https://www.linkedin.com/uas/oauth/mukeshyadav4u.blogspot.in")) {
    cancel();
   } else {
    Log.i("LinkedinSample", "url: " + url);
    view.loadUrl(url);
   }

   return true;
  }
 }

 /**
  * List of listener.
  */
 private List listeners = new ArrayList();

 /**
  * Register a callback to be invoked when authentication have finished.
  * 
  * @param data
  *            The callback that will run
  */
 public void setVerifierListener(OnVerifyListener data) {
  listeners.add(data);
 }

 /**
  * Listener for oauth_verifier.
  */
 interface OnVerifyListener {
  /**
   * invoked when authentication have finished.
   * 
   * @param verifier
   *            oauth_verifier code.
   */
  public void onVerify(String verifier);
 }
}
3: Config.java
package com.example.linkedin;

public class Config {

 public static String LINKEDIN_CONSUMER_KEY = "your consumer key here";
 public static String LINKEDIN_CONSUMER_SECRET = "your consumer secret here
";


public static String scopeParams = "rw_nus+r_basicprofile";
 
public static String OAUTH_CALLBACK_SCHEME = "x-oauthflow-linkedin";
public static String OAUTH_CALLBACK_HOST = "callback";
public static String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
}

4. main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/blue_bg_rect"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/header"
       
android:layout_width="fill_parent"
       
  android:layout_height="wrap_content"
       
  android:background="@drawable/blue_gradient_header"
        android:orientation="horizontal"
  >
        <Button
           
  android:id="@+id/logo"
           
  android:layout_width="wrap_content"
           
  android:layout_height="wrap_content"
           
  android:layout_centerHorizontal="false"
           
  android:layout_centerVertical="true"

           
  android:layout_gravity="center"
           
  android:layout_marginLeft="8dp"

           
  android:background="@drawable/logo"

           
  android:padding="4dp"
            android:text=""
  />
        <TextView
           
  android:id="@+id/txtTitle"
           
  android:layout_width="wrap_content"

           
  android:layout_height="32dp"
           
  android:layout_centerHorizontal="true"
           
  android:layout_centerVertical="true"
           
  android:text="Linkedin"
           
  android:textColor="@android:color/white"
           
  android:textSize="22sp"
           
  android:textStyle="bold"
            android:typeface="sans"
  >
        </TextView>
    </RelativeLayout>
    <TextView
        android:id="@+id/name"
       
 android:layout_width="wrap_content"
       
  android:layout_height="wrap_content"
       
  android:layout_below="@+id/header"
        android:padding="15dp"
        android:text="Name"
       
  android:textColor="@android:color/white"
        android:textSize="16sp"
        android:textStyle="bold"
        android:visibility="invisible"
 />
    <Button
        android:id="@+id/share"
       
  android:layout_width="wrap_content"
       
  android:layout_height="wrap_content"
       
  android:layout_alignParentRight="true"
       
  android:layout_below="@+id/et_share"
        android:layout_marginTop="22dp"
       
  android:background="@drawable/linkedin_share"
        android:visibility="invisible"
  />
    <Button
        android:id="@+id/login"
       
  android:layout_width="wrap_content"

       
  android:layout_height="wrap_content"

       
  android:layout_centerHorizontal="true"
       
  android:layout_centerInParent="true"
       
  android:layout_centerVertical="true"
       
  android:background="@drawable/btn_active_pressed"
       
  android:gravity="center_vertical|center_horizontal"
        android:padding="8dp"
        android:text="Login"
       
  android:textColor="@android:color/white"
        android:textStyle="bold" />
    <ImageView
        android:id="@+id/photo"
        android:layout_width="72dp"
        android:layout_height="72dp"
       
  android:layout_alignRight="@+id/et_share"
       
  android:layout_alignTop="@+id/name"

        android:layout_marginTop="22dp"

       
  android:background="@drawable/photo"

        android:padding="5dp"

        android:visibility="invisible"
  />



    <EditText
        android:id="@+id/et_share"
       
 android:layout_width="match_parent"
        android:layout_height="100dp"
       
  android:layout_alignBottom="@+id/login"
       
  android:layout_alignParentRight="true"
        android:ems="10"
       
  android:gravity="top|center_vertical"
        android:hint="Enter Your Text to
  Share"
        android:visibility="invisible"
  >

        <requestFocus />
    </EditText>
</RelativeLayout>

finally the AndroidManifest.xml file:

5. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest
  xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.linkedin"

    android:versionCode="1"

    android:versionName="1.0" >



    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="16"
  />



    <uses-permission
  android:name="android.permission.INTERNET" />

    <application

        android:allowBackup="true"

       
  android:icon="@drawable/ic_launcher"

       
  android:label="@string/app_name"

        android:theme="@style/AppTheme"
  >

        <activity

           
  android:name="com.example.linkedin.LinkedInSampleActivity"

           
  android:label="@string/app_name" >

           
  <intent-filter>

               
  <action android:name="android.intent.action.MAIN" />



               
  <category android:name="android.intent.category.LAUNCHER"
  />

           
  </intent-filter>

        </activity>

    </application>



</manifest>

NoteWhile Sending or Posting message on LinkedIn , Or shairing message on
           LinkedIn you found following error to:      


I am obtaining the following error when using the resource client.sendMessage():

com.google.code.linkedinapi.client.LinkedInApiClientException: Throttle limit for calls to this resource is reached.
com.google.code.linkedinapi.client.impl.BaseLinkedInApiClient.createLinkedInApiClientException(BaseLinkedInApiClient.java:3906)
com.google.code.linkedinapi.client.impl.BaseLinkedInApiClient.callApiMethod(BaseLinkedInApiClient.java:3846)
com.google.code.linkedinapi.client.impl.BaseLinkedInApiClient.sendMessage(BaseLinkedInApiClient.java:1829)
org.apache.jsp.linkedin_005fEmail_jsp._jspService(linkedin_005fEmail_jsp.java:149)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

This is because on LinkedIn there is a few limits, the number of message you can
share on linkedIn. Check below two links for more knowledge:

1. http://developer.linkedin.com/forum/what-throttle-limit-calls-clientsendmessage
2http://developer.linkedin.com/documents/throttle-limits---see the per day limit

Few Other links
1. http://developer.linkedin.com/documents/libraries-and-tools
2. https://code.google.com/p/linkedin-j/downloads/list



Download the Complete Source Code : LinkedInDemo

Hope this post Helps you.
Enjoy Coding :)


Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

100 comments:

  1. Thanks a lot bro...

    Awesome tutorial. it helps me a lot...
    Thanks a ton.. :) :)

    ReplyDelete
  2. Hi Mukesh,

    you have mention "R.layout.ln_dialog" but here there is no such layout. can you please provide details for this layout.

    also "R.id.webkitWebView1" is not present. it is giving error. also provide details where to place this view.

    thanks in advance.

    Ripal

    ReplyDelete
  3. Nice tuto, but you should mention libraries and tools used:

    http://developer.linkedin.com/documents/libraries-and-tools

    https://code.google.com/p/linkedin-j/downloads/list

    ReplyDelete
  4. Thanks Hatem,
    I have Update the blog with complete source code and url.
    Also now I added the git Repository link for download the code also.

    ReplyDelete
  5. Hello Ripal,
    I just Update the blog with complete source code with git repositry link , please check now.

    ReplyDelete
  6. Hi Mukesh,

    Project is not working form the above link(github). Is any way to solve the problem?. I am getting the following error

    com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null

    ReplyDelete
  7. Hello Vimal,
    Have you changed the LINKEDIN_CONSUMER_KEY and SECRET KEY??? because this is giving you a communication error.

    ReplyDelete
  8. Hi Mukesh,

    Thanks for your immediate reply.

    Yes I changed. In the debug mode the error raise in the following lines.
    file:
    ====
    LinkedIndialog.java

    Line:
    =====
    LinkedinDialog.liToken = LinkedinDialog.oAuthService.getOAuthRequestToken(Config.OAUTH_CALLBACK_URL);

    my email id is: vimal.aptinfo@gmail.com

    Thanks

    ReplyDelete
  9. Hello Vimal, I have test the above git repo code and its working fine.Please cross check once again the way you are creating the key.Or you can drop your code...so that I will check and fixed it.

    ReplyDelete
  10. Hi mukesh,
    I download your sample but unfortunately got some errors. When I click login button app is closing.
    Maybe you can help me ?

    Tried on samsung galaxy w and samsung note 10.1

    ReplyDelete
  11. Hi nice post, this code is working but am trying to share a message to only perticular connections, is this possible in android

    ReplyDelete
  12. Hello,
    Please provide me log error....

    ReplyDelete
  13. Hi,

    application crashes when hitting the "Login" button.

    ReplyDelete
  14. hi mukesh,
    i am implementing linkedin integration in my android app.
    i am getting following error

    java.lang.NoSuchMethodError: oauth.signpost.OAuthProvider.retrieveRequestToken

    ReplyDelete
  15. hi nice code, but when i am trying to run the application it gives the error that ,

    Caused by: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null

    ReplyDelete
  16. Hi,
    Am getting error log as:

    com.google.code.linkedinapi.client.LinkedInApiClientException: Access to posting network updates denied.

    at com.google.code.linkedinapi.client.impl.BaseLinkedInApiClient.createLinkedInApiClientException(BaseLinkedInApiClient.java:2100)

    can you help...

    ReplyDelete
  17. Hello Mukesh

    I am looking to POST using API's in Java. Can you help me with a sample .

    Thanks,
    Basha

    ReplyDelete
  18. Hi Mukesh, i downloaded your sample app but having similar issue. when i click login button it crashes "application linkedin is not responding" and after that gives message that app has stopped working

    giving u the link for error log:
    https://www.dropbox.com/s/wbt0im5qbm5uclb/log%20linkedin%20crash%20.txt

    ReplyDelete
  19. Hello Ayesha,

    I think you didn't change the secret key and consumer key.....please those key with the valid one.
    I have tested the code and its working at my end.
    Please let me know if you still facing same error.

    ReplyDelete
  20. @Mukesh , same error as Ayesha tariq is getting. please check.

    ReplyDelete
  21. public static String OAUTH_CALLBACK_SCHEME ="x-oauthflow-linkedin";
    public static String OAUTH_CALLBACK_HOST = "callback";
    public static String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_

    please tell me is there any change we have to made in these parameters..

    ReplyDelete
  22. Hello,
    No there is no need to change these values.

    ReplyDelete
  23. Hello Mukesh, if i try to gt any info other than user Name it crashes.
    please can you go through this issue.

    ReplyDelete
  24. Hi Mukesh. Can you tell me what version of the linkedin-j library you included in your code on github? Thanks.

    ReplyDelete
  25. Hi. Could you tell me what version of the linkedin-j library you have in your github download? It doesn't appear to match any available on Google Code.

    Thanks.

    ReplyDelete
  26. the project fails bcoz of "android.os.NetworkOnMainThreadException"

    solution:
    http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception

    ReplyDelete
  27. hi mukesh

    how can get getPublicProfileUrl

    ReplyDelete
  28. how can i put my Outh key into the app..its not taking the url passed by me

    ReplyDelete
  29. hi mukesh,
    I am getting error,

    com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null

    plz help me..

    ReplyDelete
  30. hello mukesh i have changed,...api key...and secret key..its still giving me this log cat

    10-05 15:39:20.093: E/AndroidRuntime(1195): FATAL EXCEPTION: main
    10-05 15:39:20.093: E/AndroidRuntime(1195): com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthRequestToken(LinkedInOAuthServiceImpl.java:180)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.mukesh.linkedin.LinkedinDialog.setWebView(LinkedinDialog.java:69)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.mukesh.linkedin.LinkedinDialog.onCreate(LinkedinDialog.java:55)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at android.app.Dialog.show(Dialog.java:260)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.mukesh.linkedin.LinkedInSampleActivity.linkedInLogin(LinkedInSampleActivity.java:92)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.mukesh.linkedin.LinkedInSampleActivity.access$0(LinkedInSampleActivity.java:86)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.mukesh.linkedin.LinkedInSampleActivity$1.onClick(LinkedInSampleActivity.java:61)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at android.view.View.performClick(View.java:4202)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at android.view.View$PerformClick.run(View.java:17340)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at android.os.Handler.handleCallback(Handler.java:725)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at android.os.Handler.dispatchMessage(Handler.java:92)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at android.os.Looper.loop(Looper.java:137)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at android.app.ActivityThread.main(ActivityThread.java:5039)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at java.lang.reflect.Method.invokeNative(Native Method)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at java.lang.reflect.Method.invoke(Method.java:511)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at dalvik.system.NativeStart.main(Native Method)
    10-05 15:39:20.093: E/AndroidRuntime(1195): Caused by: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null
    10-05 15:39:20.093: E/AndroidRuntime(1195): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69)
    10-05 15:39:20.093: E/AndroidRuntime(1195): at com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthRequestToken(LinkedInOAuthServiceImpl.java:172)

    ReplyDelete
  31. Hello Friends, I just Updated the code as It was crashing on sharing message on Linkedin. As it shows error "LinkedInApiClientException: Access to posting network updates denied."
    Now, It will be Fixed. I have updated the Linkedin-j-android lib and also edited few lines in the code. Now Its working perfectly.
    Thanks all, who drop me the message and mail regarding this issue.

    ReplyDelete
  32. hello mukesh, where is your updated code which you are saying in above comment.

    ReplyDelete
  33. Hi Mukesh,

    Please post your latest code.

    ReplyDelete
  34. hiiii,,,,mukesh,,,,
    provide updated code....

    ReplyDelete
  35. Hello Friends ,
    Check the updated code here..
    https://github.com/mukesh4u/Android-LinkedIn-Demo

    ReplyDelete
  36. Hi,
    I also receive the error message,

    com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null

    Can you please tell me the reason. I have modified the config file correctly.

    Thanks

    ReplyDelete
  37. Hello Madhu..
    Will u please send me your code. How you guys are trying. Bcoz, I am running the above code at my end and its not giving me any error.

    ReplyDelete
  38. Hi Mukesh,
    Thanks for quick reply. I used the code in
    https://github.com/mukesh4u/Android-LinkedIn-Demo
    Do I need to do special things for Java build path?
    I used API Key and Secret Key not OAuth User Token and OAuth User Secret, Am I correct?
    Thanks

    ReplyDelete
  39. hey mukesh your code is stop on belove code..
    LinkedinDialog.liToken = LinkedinDialog.oAuthService
    .getOAuthRequestToken(Config.OAUTH_CALLBACK_URL);
    i can't understood..

    ReplyDelete
  40. Hi Mukesh,
    I was also facing the following issue -
    com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null

    This can be fixed by placing LinkedinDialog.oAuthService.getOAuthRequestToken() in a AsyncTask.

    Thanks,
    Sanyam

    ReplyDelete
  41. I have the same problem Communication with the service provider failed: null

    ReplyDelete
  42. Hi,
    I am also facing the same error:
    com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null

    How can we place the LinkedinDialog.oAuthService.getOAuthRequestToken() in an AsyncTask?

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. Hello Richa,
    Please call the linkedInLogin() method in AsyncTask. Currently it running on main thread.

    ReplyDelete
  45. Hi Mukesh,
    i wrote the setVerifierListener method inside onPostExecute method of async task like this:

    protected void onPostExecute(Void result) {

    // TODO Auto-generated method stub

    d.setWebView();
    d.setVerifierListener(new OnVerifyListener() {
    @Override
    public void onVerify(String verifier) {
    try {
    Log.i("LinkedinSample", "verifier: " + verifier);

    accessToken = LinkedinDialog.oAuthService
    .getOAuthAccessToken(LinkedinDialog.liToken,
    verifier);
    LinkedinDialog.factory.createLinkedInApiClient(accessToken);
    client = factory.createLinkedInApiClient(accessToken);
    // client.postNetworkUpdate("Testing by Richa!!! LinkedIn wall post from Android app");
    Log.i("LinkedinSample",
    "ln_access_token: " + accessToken.getToken());
    Log.i("LinkedinSample",
    "ln_access_token: " + accessToken.getTokenSecret());
    Person p = client.getProfileForCurrentUser();
    name.setText("Welcome " + p.getFirstName() + " "
    + p.getLastName());
    name.setVisibility(0);
    login.setVisibility(4);
    share.setVisibility(0);
    et.setVisibility(0);

    } catch (Exception e) {
    Log.i("LinkedinSample", "error to get verifier");
    e.printStackTrace();
    }
    }
    });
    }

    an error is thrown oin the catch block and it says unable to get verifier. After providing the login details, I am again redirected to the login page instead of the welcome screen. Where am i going wrong?

    ReplyDelete
  46. Hello Richa,
    Please provide me the log cat error.

    ReplyDelete
  47. Working Fine Thanks a lot....................

    ReplyDelete
  48. helo mukesh ... can u update ur code on github which include async task ... im trying but not getting

    ReplyDelete
  49. Hello Mukesh, I was also getting OAuthServiceException but resolved it by placing the code in AsyncTask. Now I am getting LinkedInApiClientException on the line client.getProfileForCurrentUser().
    Kindly let me know reason for the exception and possible solution for the same. Thanks in advance.

    ReplyDelete
  50. I have download the code from the above link.
    Its working fine but getting following exception while integrating into my application
    "E/StrictMode(622): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    E/StrictMode(622): java.lang.Throwable: Explicit termination method 'end' not called
    " .
    Can you please suggest me for this?

    ReplyDelete
  51. its working on jelly bean but its not working on 4.0.3 and below os
    So please help

    ReplyDelete
  52. Thanx worked for me but getPhoneNumbers() returns null, please help.....

    ReplyDelete
  53. I am trying to integrate linkedin with android application. After giving the login credentials, access token getting null due to below exception.

    com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null

    If I will change my code with async task, It will work or not ?? @Mukesh yadav or anyone help me..

    ReplyDelete
  54. Hello SUNDARAMOORTHY,
    When you place the code inside AsyncTask it will work.

    ReplyDelete
  55. Thank you Mukesh yadav..
    Now working fine, successfully logged in. But NetworkOnMainThread exception. I have an idea to resolve this. I need to put share method also within async task. Am I right ?

    ReplyDelete
  56. Yes , use asyn task for sharing message on LinkedIn. Actually this NetworkOnMainThread issue is because of "StrictMode.ThreadPolicy".
    Use asyn task for sharing too.

    ReplyDelete
  57. I have moved my share method code to Async task and run the application. Toast showed "Shared successfully". But there is no status update in my linkedin profile. Here whats happening ? Is there any callback method for share functionality ? Because we dont know whether the shared text will reach the profile or not ? Or else is there any other way to share the text ?
    @Mukesh Yadav

    ReplyDelete
  58. Hello, Mukesh,

    This is really nice post.

    Issue 2 : you code is working below 4.0 only. Can you make it update for all android version.

    Issue 2 : in current when i am sending post it display toast" Share successfully" but there is nothing post in My Linked Account.

    it is really good for us if you correct it and update soon.
    Thanks

    ReplyDelete
  59. Thanks Hasmukh,
    I am checking the code...the message sharing parts is working fine at my end.
    Now fixing for higher version support.

    ReplyDelete
    Replies
    1. Thanks For your immediately reply.
      Please update soon if you can,
      waiting for your feedback....
      thanks

      Delete
  60. HI mukesh . your code running fine , no error is there but when I am posting any comment it is not showing my linkedIn account.

    ReplyDelete
  61. Hi Mukesh..
    Next I am going to get the information from Linkedin profile using People API.
    There is a java method available in linkedin API document. I want to use that method to get response. But in our project, that classes not supported. That classes not available in our application. What can I do ? Refer this link http://developer.linkedin.com/documents/reading-data.

    ReplyDelete
  62. Hello Friends,
    I did the quick fix for LinkedIn message sharing .
    I change code inside the click of share button .Please
    check the latest code on Git

    Now, it working.
    Am also looking more into it.Since LinkedIn developers
    made several changes in threre Api.

    ReplyDelete
  63. Guys , I fixed the issue of LinkedIn Post on Wall and committed on git
    repo. Please check the latest code.

    ReplyDelete
    Replies
    1. Hello Mukesh,
      Yes, i have downloaded latest source code from below url

      https://github.com/mukesh4u/Android-LinkedIn-Demo

      Wall post issue is now solved in this code.
      just only higher version issue is there.

      Thanks

      Delete
  64. Hello Mukesh,

    i have test your code and it work like charm in 2.3
    still it is not working on 4.0 or above version. here i am pasting Logcat error,
    Please look on this so you can get idea for solution of error.

    03-01 19:23:45.521: E/AndroidRuntime(16854): com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: https://api.linkedin.com/uas/oauth/requestToken?scope=rw_nus+r_basicprofile


    03-01 19:23:45.521: E/AndroidRuntime(16854): Caused by: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: https://api.linkedin.com/uas/oauth/requestToken?scope=rw_nus+r_basicprofile


    03-01 19:23:45.521: E/AndroidRuntime(16854): Caused by: java.io.FileNotFoundException: https://api.linkedin.com/uas/oauth/requestToken?scope=rw_nus+r_basicprofile

    Thanks.

    ReplyDelete
  65. Hello Mukesh,
    i have resolved this error, Now it work superb.

    there is a mismatch parameter in scopeParams.
    public static String scopeParams="rw_nus+r_basicprofile";

    here we have we have to set same parameter which is done check mark while creating application.

    Again,Thank you very much for helping and share this post.

    ReplyDelete
  66. Hello Mukesh,
    This application based on oAuth 1.0 or oAuth 2.0 ? which one you used ?

    ReplyDelete
  67. Hello Mukesh,
    Is there any way to logout this application ?

    ReplyDelete
  68. hello, thanks for the tutorial...it really helped .... can you please throw some light on updating user data like first name, last name etc using rest api in androi?? because in linkedin app for android...there is the provision to update data like name, profile picture etc... so please suggest the api for the same..thanks

    ReplyDelete
  69. Hello Mukesh,
    Can you give me the code how to get LinkedIn connections List. This code is working fine for me. . .
    Please help thanks in advance

    ReplyDelete
  70. can any one tell me how can i get messages from linked in android? basically i want to recieve messages in my app.. plzz guide me

    ReplyDelete
  71. Hello Mukesh,
    How to share images on linkedin wall ?
    have you any idea then please help me for share image on linkedin

    ReplyDelete
  72. Hello mukesh,

    Can you give me suggestion how to get log in email id...Please help thanks in advance......

    ReplyDelete
  73. Hello Mukesh,
    Is there any way to get email id in this application ?

    ReplyDelete
  74. Hello mukesh,

    can you give me code how to get email id, i try many ways i did't get email please give suggestion for this issue...
    help me thanks in advance,........


    ReplyDelete
  75. Hi Mukesh,

    Thanks a lot for this post. It is very useful for me.
    Can you please help me out to find linked in friend list ?

    ReplyDelete
  76. Hi

    This example is working nice but i want to get user connections list.

    i use the following url for get user connections

    HttpGet post = new HttpGet("http://api.linkedin.com/v1/people/~/connections");

    but i gives me error as below



    403
    1413290454636
    ZGXBM3Z1AH
    0
    Access to connections denied


    i try many times for changes permission in app created in linkedin and also pass permission to url but not get any luck pls help



    ReplyDelete
  77. thanks for tutorial mukesh but how to fetch user email id?

    ReplyDelete
  78. Hi Mukesh,
    I am trying to fetch industry, country,dob ect.
    Its giving nullPointerException.

    ReplyDelete
  79. The only change I did was use the latest version of linkedin-j-1.0.429 jar

    ReplyDelete
  80. The only change I did was use the latest version of linkedin-j-1.0.429 jar

    ReplyDelete
  81. Thanks Amalan,
    I fixed and updated the code on github.
    Also added some new feature in it.like user profile picturemnumber and more.

    ReplyDelete
    Replies
    1. hi mukesh,

      Thanks for this app.

      I run this app on device successfully. Also got authentication(oauth)successfully.but when is send message from this app to linked IN.and then check it on my linked IN account there is no post(msg) is displayed.

      how to check message send from app is posted on account or no not?


      Delete
  82. Hi Mukesh,
    I am using your code for sharing post on Linkedin, Yet i am successfully able to logged in but then even after getting '"Success" in sharing post, the post is not being shared to LinkedIn –
    I have tried appending scope to oAuth service params but using that, I am facing issue at Login click , "com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: https://api.linkedin.com/uas/oauth/requestToken?scope=rw_nus+r_basicprofile"

    It would be really helpful if you could tell where the problem is.

    Thanks

    ReplyDelete
  83. Hi Mukesh...
    Thanks for the tutorial.
    I can now login in my app using linkedin.
    Can you please tell me how I can log out from linkedin.

    Thanks a lot.

    ReplyDelete
  84. Mukesh please tell me how to get email_address,dateofbirth and in your code
    Config.java have scopeParams string that is not used then how permissions work
    plzzzz reply as soon as possible?????

    ReplyDelete
  85. Hlo Mukesh plzz tell me how to give permissions to retrieve user data like email and in your Config.java String scopeParams is not using then how permissions Like r_basicprofile work????

    ReplyDelete
  86. HELLO Mukesh ,M not able to get date of birth and its showing null even I given the authority to Everyone to see my birthDate.......One thing more tell me how to give r_Fullprofile permission in your code ans I checked your Config.java,in this string scopeParams not using why???plzzz reply as soon as possible ..
    thnx in advance

    ReplyDelete
  87. Hi Mukesh the code that you provided is not working correctly for me. I am not able to verify it by the code that is generated in it...Please help me.

    ReplyDelete
    Replies
    1. Hello Parth,
      Download complete from the git link:
      https://github.com/mukesh4u/Android-LinkedIn-Demo

      Delete
  88. hi mukesh , how can i get email address. please help me.

    ReplyDelete
  89. Hi,Mukesh how can i get the email address.please help me.

    ReplyDelete
  90. Hello Mukesh do you have code to sync the calendar with sqlite database?

    ReplyDelete

 

Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin