3

i have been trying to integrate Login LInkedIn from my android app. So far i have searched but there is limited examples on how to do it. but one fine answer from this question Posting LinkedIn message from Android application gave me a headstart.

but when i run it. it takes me to the login screen and then when i enter my credentials and press "Sign in and allow" it does something in browser but nothing happens. it does not log in nor it returns to my application.

Here is my Code:

final SharedPreferences pref = getSharedPreferences(OAUTH_PREF,
                MODE_PRIVATE);
        final String token = pref.getString(PREF_TOKEN, null);
        final String tokenSecret = pref.getString(PREF_TOKENSECRET, null);
        if (token == null || tokenSecret == null) {
            new AsyncTask<Void, Void, LinkedInRequestToken>() {

                @Override
                protected LinkedInRequestToken doInBackground(Void... params) {
                    return oAuthService.getOAuthRequestToken(OAUTH_CALLBACK_URL);
                }

                @Override
                protected void onPostExecute(LinkedInRequestToken liToken) {
                    final String uri = liToken.getAuthorizationUrl();
                    getSharedPreferences(OAUTH_PREF, MODE_PRIVATE)
                            .edit()
                            .putString(PREF_REQTOKENSECRET,
                                    liToken.getTokenSecret()).commit();
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                    startActivity(i);
                }
            }.execute();
        } else {
            showCurrentUser(new LinkedInAccessToken(token, tokenSecret));
        }final LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory
                .getInstance().createLinkedInOAuthService(CONSUMER_KEY,
                        CONSUMER_SECRET);

////////////////////////////REMAINING CODE//////////////////

    void finishAuthenticate(final Uri uri) {
    if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) {
        final String problem = uri.getQueryParameter(OAUTH_QUERY_PROBLEM);
        if (problem == null) {

            new AsyncTask<Void, Void, LinkedInAccessToken>() {

                @Override
                protected LinkedInAccessToken doInBackground(Void... params) {
                    final SharedPreferences pref = getSharedPreferences(
                            OAUTH_PREF, MODE_PRIVATE);
                    final LinkedInAccessToken accessToken = oAuthService
                            .getOAuthAccessToken(
                                    new LinkedInRequestToken(
                                            uri.getQueryParameter(OAUTH_QUERY_TOKEN),
                                            pref.getString(
                                                    PREF_REQTOKENSECRET,
                                                    null)),
                                    uri.getQueryParameter(OAUTH_QUERY_VERIFIER));
                    pref.edit()
                            .putString(PREF_TOKEN, accessToken.getToken())
                            .putString(PREF_TOKENSECRET,
                                    accessToken.getTokenSecret())
                            .remove(PREF_REQTOKENSECRET).commit();
                    return accessToken;
                }

                @Override
                protected void onPostExecute(LinkedInAccessToken accessToken) {
                    showCurrentUser(accessToken);
                }
            }.execute();

        } else {
            Toast.makeText(this,
                    "Appliaction down due OAuth problem: " + problem,
                    Toast.LENGTH_LONG).show();
            finish();
        }

    }
}

void clearTokens() {
    getSharedPreferences(OAUTH_PREF, MODE_PRIVATE).edit()
            .remove(PREF_TOKEN).remove(PREF_TOKENSECRET)
            .remove(PREF_REQTOKENSECRET).commit();
}
void showCurrentUser(final LinkedInAccessToken accessToken) {
    final LinkedInApiClient client = factory
            .createLinkedInApiClient(accessToken);

    new AsyncTask<Void, Void, Object>() {

        @Override
        protected Object doInBackground(Void... params) {
            try {

                final Person p = client.getProfileForCurrentUser();
                // /////////////////////////////////////////////////////////
                // here you can do client API calls ...
                // client.postComment(arg0, arg1);
                // client.updateCurrentStatus(arg0);
                // or any other API call
                // (this sample only check for current user
                // and pass it to onPostExecute)
                // /////////////////////////////////////////////////////////
                return p;
            } catch (LinkedInApiClientException ex) {
                return ex;
            }
        }

        @Override
        protected void onPostExecute(Object result) {
            if (result instanceof Exception) {
                //result is an Exception :)
                final Exception ex = (Exception) result;
                clearTokens();
                Toast.makeText(
                        getApplicationContext(),
                        "Appliaction down due LinkedInApiClientException: "
                                + ex.getMessage()
                                + " Authokens cleared - try run application again.",
                        Toast.LENGTH_LONG).show();
                finish();
            } else if (result instanceof Person) {
                final Person p = (Person) result;
                Toast.makeText(
                        getApplicationContext(),p.getLastName() + ", " + p.getFirstName(),
                        Toast.LENGTH_LONG).show();
            }
        }
    }.execute();

}

//// THE END LOG OF MY APP IS///////////

08-23 12:31:34.010  32724-32728/com.example.montovaapp D/-heap﹕ GC_CONCURRENT freed 251K, 5% free 8598K/9031K, paused 12ms+154ms, total 221ms

08-23 12:31:35.790  32724-32728/com.example.montovaapp D/-heap﹕ GC_CONCURRENT freed 857K, 12% free 8231K/9287K, paused 12ms+156ms, total 215ms

08-23 12:31:37.290  32724-32728/com.example.montovaapp D/-heap﹕ GC_CONCURRENT freed 261K, 10% free 8383K/9287K, paused 12ms+33ms, total 69ms

08-23 12:31:39.410  32724-32728/com.example.montovaapp D/-heap﹕ GC_CONCURRENT freed 255K, 9% free 8526K/9287K, paused 12ms+14ms, total 50ms

08-23 12:31:43.160  32724-32724/com.example.montovaapp W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection

Just a side note that em programming using Android Studio

regards

Community
  • 1
  • 1

1 Answers1

2

I'm sorry but maybe using ASNE library and asne-linkedin make your life easier? Just add dependency to your project:

dependencies {
...
    compile 'com.github.asne:asne-linkedin:0.2.0'
...
}

setup and request login like

socialNetwork.requestLogin();

And you can easily add another social network or other request...

Seko
  • 670
  • 1
  • 9
  • 19