Capture basic profile data Linkedin

Asked

Viewed 185 times

3

I am doing an integration of my app with Linkedin, what happens is that I can perform the authentication and return to the Activity from my app it showed me the logged in user data.

I am using the codes of the Linkedin API, but being still starting in the development I do not know where and how to call the method that recovers the data, below my code.

Can you help me with this? I just want to know one way to get the user’s name and last name logged in.

Thanks in advance.

package com.example.testeli;

import com.linkedin.platform.LISession;
import com.linkedin.platform.LISessionManager;
import com.linkedin.platform.errors.APIHelper;
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 android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.testeli.R;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        final Activity thisActivity = this;

        Button linkedin = (Button) findViewById(R.id.linkedin);

        linkedin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                LISessionManager.getInstance(getApplicationContext()).init(
                        thisActivity, buildScope(), new AuthListener() {

                            @Override
                            public void onAuthSuccess() {
                                // Authentication was successful. You can now do
                                // other calls with the SDK.

                            }

                            @Override
                            public void onAuthError(LIAuthError error) {
                                // Handle authentication errors
                            }

                        }, true);

            }
        });

    }

    // Build the list of member permissions our LinkedIn session requires
    private static Scope buildScope() {
        return Scope.build(Scope.R_BASICPROFILE, Scope.W_SHARE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Add this line to your existing onActivityResult() method
        LISessionManager.getInstance(getApplicationContext()).onActivityResult(
                this, requestCode, resultCode, data);
    }

    public void getProfile() {
        String url = "https://api.linkedin.com/v1/people/~?format=json:(id,first-name,last-name)";

        APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
        apiHelper.getRequest(this, url, new ApiListener() {
            @Override
            public void onApiSuccess(ApiResponse s) {
                onApiSuccess(s);

            }

            @Override
            public void onApiError(LIApiError LIApiError) {
                // TODO Auto-generated method stub

            }

        });

    }
}

1 answer

1

I managed to solve my problem, as stated above I am using the Linkedin API where there is a class called Apiresponse, so I understand it already performs all the treatment of JSON.

I hope that if someone faces the same problem the code snippet below can help.

public void getProfile() {
    String url = "https://api.linkedin.com/v1/people/~";

    APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
    apiHelper.getRequest(this, url, new ApiListener() {
        @Override
        public void onApiSuccess(ApiResponse s) {
            // onApiSuccess(s);
            JSONObject json = s.getResponseDataAsJson();
            try {
                String nome = json.getString("firstName");
                String sobrenome = json.getString("lastName");
                // JSONObject nome = json.getJSONObject("lastName");
                // nome.toString();

                System.out.println(nome);
                System.out.println(sobrenome);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        @Override
        public void onApiError(LIApiError LIApiError) {
            // TODO Auto-generated method stub

        }

    });

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.