Saturday, July 28, 2012
This article demonstrates Android LinkedIn OAuth implementation using the Scribe library. Implements a sample application "MyConnections".

“MyConnections” application

Intent of “MyConnections” application is to connect to LinkedIn and display all my connections on the Android platform. Primarily with this application we would be able to demonstrate:-

  1. Android LinkedIn OAuth implementation which is explained in this article.

  2. Android LinkedIn connection implementation using the REST APIs provided by LinkedIn which is explained in a subsequent article.

Register an application as LinkedIn developer

First step in connecting to LinkedIn is to register the application in LinkedIn Developer Portal and get the API key and API secret. For the purpose of this example, I have already registered an application “MyConnections” and have the API key and secret with me.

Scribe OAuth Library

Scribe is a simple OAuth library for Java and supports LinkedIn OAuth APIs out of the box. Scribe is Android ready. More details on Scribe at this location Scribe link. This example uses Scribe to access LinkedIn.

Scribe 1.3.0 is used by this application. Follow these steps to include scribe library to the project.

  1. Create a libs/ folder in the Android application folder and put in scribe-1.3.0.jar file.

  2. Fix the build path of the project to include the scribe library.

  3. Right click on the Android project in eclipse to select “Build Path”.

  4. Now select the sub-option “Configure Build Path”.

  5. Select “Add JARs” and choose the copied library path to insert scribe-1.3.0.jar to the project.

LinkedIn Login Activity

This application has a login activity which has a simple login button. The XML layout of login activity is listed below. We have an authenticate button which initiates the OAuth flow.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="Login LinkedIn" />

The OnClickListener of authenticate button triggers another activity which performs OAuth with LinkedIn.

LinkedIn OAuth Activity

The OAuth activity has a web view which loads and displays the LinkedIn authentication screen. The XML layout is simple and looks as below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

    <android.webkit.WebView android:id="@+id/linkedin_webview" 


All the OAuth related work happens in the activity implementation. The OAuth activity has the following API key, secret and callback defined.
final static String APIKEY = "xxxxxxxxxxxx";
final static String APISECRET = "xxxxxxxxxxxxxxxx";
final static String CALLBACK = "oauth://linkedin";

Scribe has the simple service builder API on which we execute the LinkedIn OAuth workflow.
mService = new ServiceBuilder()

Also one important point to note is that to prevent android.os.NetworkOnMainThreadException exception it becomes necessary to exercise the OAuth flow in an AsyncTask.
private class LinkedInAuthTask extends AsyncTask<Void, Void, String> {

In the doInBackground of AsyncTask we fetch the request token and authorize scribe. This method returns the authURL which is loaded in the web view.
protected String doInBackground(Void... arg0) {

// Temporary URL
String authURL = "";

try {
        mRequestToken = mService.getRequestToken();
 authURL = mService.getAuthorizationUrl(mRequestToken);
catch ( OAuthException e ) {
 return null;

return authURL;

In the onPostExecute we load the authURL in the web view. At this point user is prompted to key in her LinkedIn credentials and grant access. On receiving the required callback we trade the request token for access token and finish the activity.
protected void onPostExecute(String authURL) { 
    mWebView.setWebViewClient(new WebViewClient() {

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
 super.shouldOverrideUrlLoading(view, url);

 if( url.startsWith("oauth") ) {

     final String url1 = url;
        Thread t1 = new Thread() {
  public void run() {
        Uri uri = Uri.parse(url1);

      String verifier = uri.getQueryParameter("oauth_verifier");
      Verifier v = new Verifier(verifier);
      Token accessToken = mService.getAccessToken(mRequestToken, v);          
      Intent intent = new Intent();
      intent.putExtra("access_token", accessToken.getToken());
      intent.putExtra("access_secret", accessToken.getSecret());
       setResult(RESULT_OK, intent);


 return false;


It also becomes necessary to store the access token and access secret to prevent the application from re-prompting the credentials. It is common to store the access token and secrets in shared preferences and reuse for the next launch.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);

     if ( resultCode == RESULT_OK && requestCode == LINKEDIN_LOGIN_RESULT_CODE ) {
      String access_token = data.getStringExtra("access_token");
      String access_secret = data.getStringExtra("access_secret");

      // Store the tokens in preferences for further use
      SharedPreferences.Editor editor = mPrefs.edit();
      editor.putString("linkedin_access_token", access_token);
      editor.putString("linkedin_access_secret", access_secret);

      // Start activity
      Intent intent = new Intent(this, LinkedInListActivity.class);


  1. Hello I'm developing an Android App and I want to log in linkedIn, but I don't know how, because I always have the same error:

    Caused by: org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: 'oauth_problem=timestamp_refused&oauth_acceptable_timestamps=1349427104%2B-300'

    that is produced on this line:
    Token requestToken = service.getRequestToken();

    why? can you help me please?

  2. Great example! Would you be able to post the complete classes for the examples above?

  3. Hi.
    Noticed that we need to set the scope value as well when building the scribe service builder.

    mService = new ServiceBuilder().provider(LinkedInApi.class)

    Hope it helps.


  4. Yes, could you please post the complete classes for this example? It would be much appreciated. Thanks!


Contact Form


Email *

Message *

Back to Top