Skip to main content

ANDROID - update status Linked-IN


jars:

1. linkedin-j-android.jar
2. scribe-1.3.1.jar

MainActivity:

 package com.testshare;  
 import java.util.EnumSet;  
 import org.scribe.oauth.OAuthService;  
 import android.app.Activity;  
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.util.Log;  
 import com.google.code.linkedinapi.client.LinkedInApiClient;  
 import com.google.code.linkedinapi.client.LinkedInApiClientFactory;  
 import com.google.code.linkedinapi.client.enumeration.NetworkUpdateType;  
 import com.google.code.linkedinapi.schema.Network;  
 public class TestshareActivity extends Activity {  
        private int LINKEDIN_OAUTH_RESULT_CODE = 4000;  
        private OAuthService service;  
        private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~";  
        /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.main);  
               click();  
        }  
        public void click() {  
               Intent intent = new Intent(TestshareActivity.this,  
                             LinkedInOAuthActivity.class);  
               startActivityForResult(intent, LINKEDIN_OAUTH_RESULT_CODE);  
        }  
        @Override  
        protected void onRestart() {  
               // TODO Auto-generated method stub  
               shareText_new();  
               super.onRestart();  
        }  
        private void shareText_new() {  
               // String scopeParams = "rw_nus+r_basicprofile";  
               // TODO Auto-generated method stub  
               try {  
                      final LinkedInApiClientFactory factory = LinkedInApiClientFactory  
                                    .newInstance(generalClass.APIKEY, generalClass.APISECRET);  
                      final LinkedInApiClient client = factory.createLinkedInApiClient(  
                                    generalClass._Token1, generalClass._Secret1);  
                      client.postNetworkUpdate("hello DJ-android.blogspot.com");  
                      System.out  
                                    .println("Your update has been posted. Check the LinkedIn site for confirmation.");  
                      System.out.println("Fetching your network updates of type:"  
                                    + NetworkUpdateType.STATUS_UPDATE);  
                      Network network = client.getNetworkUpdates(EnumSet  
                                    .of(NetworkUpdateType.STATUS_UPDATE));  
                      // printResult(network);  
               } catch (Exception e) {  
                      // TODO: handle exception  
                      Log.e("error share st--->", "" + e.getMessage().toString());  
               }  
        }  
 }  

LinkedInOAuthActivity

 package com.testshare;  
 import android.app.Activity;  
 import android.content.Intent;  
 import android.net.Uri;  
 import android.os.AsyncTask;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.webkit.WebView;  
 import android.webkit.WebViewClient;  
 import org.scribe.builder.ServiceBuilder;  
 import org.scribe.builder.api.LinkedInApi;  
 import org.scribe.exceptions.OAuthException;  
 import org.scribe.model.*;  
 import org.scribe.oauth.*;  
 public class LinkedInOAuthActivity extends Activity {  
        private WebView mWebView = null;  
        private OAuthService mService = null;  
        private Token mRequestToken = null;  
        String scopeParams = "rw_nus+r_baseprofile";  
        String scopeParams1 = "rw_nus";  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.linkedin_oauth);  
               mService = new ServiceBuilder().provider(LinkedInApi.class)  
                             .apiKey(generalClass.APIKEY).apiSecret(generalClass.APISECRET)  
                             .callback(generalClass.CALLBACK).scope(scopeParams1).build();  
               Log.e("mService----->", "" + mService);  
               mWebView = (WebView) findViewById(R.id.linkedin_webview);  
               // Start the async task  
               LinkedInAuthTask task = new LinkedInAuthTask();  
               task.execute();  
        }  
        // Async task for authentication  
        private class LinkedInAuthTask extends AsyncTask<Void, Void, String> {  
               @Override  
               protected String doInBackground(Void... arg0) {  
                      // Temporary URL  
                      String authURL = "http://api.linkedin.com/";  
                      try {  
                             mRequestToken = mService.getRequestToken();  
                             Log.e("mRequestToken----->", "" + mRequestToken);  
                             // mService.getAccessToken(arg0, arg1)  
                             authURL = mService.getAuthorizationUrl(mRequestToken);  
                             Log.e("authURL----->", "" + authURL);  
                      } catch (OAuthException e) {  
                             e.printStackTrace();  
                             return null;  
                      }  
                      return authURL;  
               }  
               @Override  
               protected void onPostExecute(String authURL) {  
                      mWebView.setWebViewClient(new WebViewClient() {  
                             @Override  
                             public boolean shouldOverrideUrlLoading(WebView view, String url) {  
                                    super.shouldOverrideUrlLoading(view, url);  
                                    if (url.startsWith("oauth")) {  
                                           mWebView.setVisibility(WebView.GONE);  
                                           final String url1 = url;  
                                           Thread t1 = new Thread() {  
                                                  public void run() {  
                                                         Uri uri = Uri.parse(url1);  
                                                         String verifier = uri  
                                                                       .getQueryParameter("oauth_verifier");  
                                                         Log.e("verifier----->", "" + verifier);  
                                                         generalClass._verifier = verifier;  
                                                         Verifier v = new Verifier(verifier);  
                                                         Token accessToken = mService.getAccessToken(  
                                                                       mRequestToken, v);  
                                                         // Token accessToken1 =  
                                                         // request.getSession().setAttribute("requestToken",  
                                                         // v);  
                                                         Log.e("accessToken.getToken()----->", ""  
                                                                       + accessToken.getToken());  
                                                         Log.e("accessToken.getSecret()----->", ""  
                                                                       + accessToken.getSecret());  
                                                         // Token a = accessToken;  
                                                         generalClass._Token = accessToken;  
                                                         generalClass._Token1 = accessToken.getToken();  
                                                         generalClass._Secret1 = accessToken.getSecret();  
                                                         Intent intent = new Intent();  
                                                         intent.putExtra("access_token",  
                                                                       accessToken.getToken());  
                                                         intent.putExtra("access_secret",  
                                                                       accessToken.getSecret());  
                                                         setResult(RESULT_OK, intent);  
                                                         finish();  
                                                  }  
                                           };  
                                           t1.start();  
                                    }  
                                    return false;  
                             }  
                      });  
                      mWebView.loadUrl(authURL);  
               }  
        }  
 }  

linkedin_oauth.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent" android:layout_height="fill_parent"  
        android:orientation="vertical">  
        <WebView android:id="@+id/linkedin_webview" android:focusable="true"  
               android:scrollbars="vertical" android:layout_width="match_parent"  
               android:layout_height="match_parent"></WebView>  
 </LinearLayout>  

extraClass

 package com.testshare;  
 import org.scribe.model.Token;  
 public class generalClass {  
        public final static String APIKEY = "linkedin API KEY";  
        public final static String APISECRET = "linkedin API SECRET";  
        public final static String CALLBACK = "oauth://linkedin";  
        public static String _verifier;  
        public static Token _Token;  
        public static String _Secret;  
        public static String _Token1;  
        public static String _Secret1;  
 }  

manifest File

 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
        package="com.testshare" android:versionCode="1" android:versionName="1.0">  
        <uses-sdk android:maxSdkVersion="13" android:minSdkVersion="8" />  
        <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>  
        <application android:icon="@drawable/icon" android:label="@string/app_name">  
               <activity android:name=".TestshareActivity" android:label="@string/app_name">  
                      <intent-filter>  
                             <action android:name="android.intent.action.MAIN" />  
                             <category android:name="android.intent.category.LAUNCHER" />  
                      </intent-filter>  
               </activity>  
               <activity android:name=".LinkedInOAuthActivity"  
                      android:theme="@android:style/Theme.Light" android:configChanges="keyboardHidden|orientation"></activity>  
        </application>  
 </manifest>  

reference links:

http://code.google.com/p/linkedin-j/source/browse/trunk/linkedin-j/core/src/examples/java/com/google/code/linkedinapi/client/examples/PostNetworkUpdateExample.java?r=197

http://stackoverflow.com/questions/15082693/android-update-status-linked-in-in-apps-background

https://github.com/fernandezpablo85/scribe-java

http://stackoverflow.com/questions/12188341/cant-post-anything-on-linkedin-using-linkedin-j

Comments

  1. Hi,
    i am getting the exception at this line,
    client.postNetworkUpdate("hello DJ-android.blogspot.com");


    Please provide the solution ASAP.
    Thank

    ReplyDelete
    Replies
    1. which type of exception you got?? Please show me error Logs.

      Delete
    2. It is working fine
      But updates are not posted onto LinkedIn.
      What are the library's i need to add.

      Delete
    3. 04-17 17:01:32.446: E/error share st--->(27313): Throttle limit for calls to this resource is reached.

      after repeated posts i am getting this exception.

      Please give the clarification,Thank You.

      Delete
  2. hi Dhaval Sodha parmar i'm getting thid kind of Exception can u guide me where i'm doing wrong..plz

    05-14 12:02:36.888: I/dalvikvm(395): Could not find method com.google.code.linkedinapi.client.LinkedInApiClientFactory.newInstance, referenced from method com.testshare.TestshareActivity.shareText_new
    05-14 12:02:36.897: W/dalvikvm(395): VFY: unable to resolve static method 3395: Lcom/google/code/linkedinapi/client/LinkedInApiClientFactory;.newInstance (Ljava/lang/String;Ljava/lang/String;)Lcom/google/code/linkedinapi/client/LinkedInApiClientFactory;
    05-14 12:02:36.897: D/dalvikvm(395): VFY: replacing opcode 0x71 at 0x0004
    05-14 12:02:36.947: D/dalvikvm(395): VFY: dead code 0x0007-003b in Lcom/testshare/TestshareActivity;.shareText_new ()V
    05-14 12:02:37.507: E/mService----->(395): org.scribe.oauth.OAuth10aServiceImpl@45f55d60
    05-14 12:02:43.997: D/dalvikvm(395): GC_FOR_MALLOC freed 4962 objects / 278464 bytes in 93ms
    05-14 12:02:44.537: E/mRequestToken----->(395): Token[3b901ab4-b99f-46f9-901f-06d5a58833e3 , 96e587ee-3254-4453-91bc-98e4a5e375cc]
    05-14 12:02:44.537: E/authURL----->(395): https://api.linkedin.com/uas/oauth/authorize?oauth_token=3b901ab4-b99f-46f9-901f-06d5a58833e3
    05-14 12:03:39.047: E/accessToken.getToken()----->(395): 51ae571b-1263-42f7-8e0b-c93dfef508af
    05-14 12:03:39.047: E/accessToken.getSecret()----->(395): b0e061d1-0d0f-47b9-9dce-12b6fcf527bc
    05-14 12:03:39.097: D/NativeCrypto(395): Freeing OpenSSL session
    05-14 12:03:39.117: D/AndroidRuntime(395): Shutting down VM
    05-14 12:03:39.117: W/dalvikvm(395): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
    05-14 12:03:39.167: E/AndroidRuntime(395): FATAL EXCEPTION: main
    05-14 12:03:39.167: E/AndroidRuntime(395): java.lang.NoClassDefFoundError: com.google.code.linkedinapi.client.LinkedInApiClientFactory
    05-14 12:03:39.167: E/AndroidRuntime(395): at com.testshare.TestshareActivity.shareText_new(TestshareActivity.java:39)
    05-14 12:03:39.167: E/AndroidRuntime(395): at com.testshare.TestshareActivity.onRestart(TestshareActivity.java:31)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1139)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.app.Activity.performRestart(Activity.java:3805)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.app.Activity.performResume(Activity.java:3816)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2059)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.os.Handler.dispatchMessage(Handler.java:99)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.os.Looper.loop(Looper.java:123)
    05-14 12:03:39.167: E/AndroidRuntime(395): at android.app.ActivityThread.main(ActivityThread.java:4627)
    05-14 12:03:39.167: E/AndroidRuntime(395): at java.lang.reflect.Method.invokeNative(Native Method)
    05-14 12:03:39.167: E/AndroidRuntime(395): at java.lang.reflect.Method.invoke(Method.java:521)
    05-14 12:03:39.167: E/AndroidRuntime(395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    05-14 12:03:39.167: E/AndroidRuntime(395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    05-14 12:03:39.167: E/AndroidRuntime(395): at dalvik.system.NativeStart.main(Native Method)

    ReplyDelete
  3. i'm getting this kind of error what is this "Throttle limit for calls to this resource is reached."

    ReplyDelete
  4. Yes ...it's working but can't see post on LinkedIn.

    ReplyDelete
  5. According to LinkedIn documentation->

    This is the only API that has a whitelist. While your application is in beta mode, network updates you send are posted only to the developers on your whitelist who are also first degree connections. When you indicate that your application is live, network updates will then go to the entire first degree connections list of the user who posts them.

    So make your App Live

    ReplyDelete
  6. according to documentation:-

    Whitelist

    This is the only API that has a whitelist. While your application is in beta mode, network updates you send are posted only to the developers on your whitelist who are also first degree connections. When you indicate that your application is live, network updates will then go to the entire first degree connections list of the user who posts them.

    You toggle your application's status between Development or Live by visiting the API Provisioning page and setting the Application Status field. You can also add and remove developers to your application on that page.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

Android - Google MAP V2 PART 3 (add Polyline)

Download Full Code  of  PART-1 , 2 , 3 , 4 Before go ahead Please look in to PART-1 & PART-2 I have just modify Main Activity class: package com.djandroid.mapsv2; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; public class MainActivity extends FragmentActivity { private GoogleMap MAP; private boolean markClic...

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l...

Connecting mysql Database in ANDROID using PHP & JSON

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image. Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL: CREATE TABLE `employee` (   `emp_id` int(11) NOT NULL auto_increment,   `emp_name` varchar(100) NOT NULL,   PRIMARY KEY  (`emp_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; The PHP code will be very simple: Now Create Android Project :  The Android part is only a bit more complicated: -use a HttpPost to get the data -convert response to string -parse JSON data in to List In Your First Activity : O/P :