Capturing basic profile data Linkedin

3

I'm doing an integration of my app with Linkedin, what happens is that I can perform the authentication and on return to Activity of my app it would show me the data of the logged in user.

I'm using Linkedin APIs, but because I'm still starting development, I do not know where and how to call the method that retrieves the data, below my code.

Can you help me with this? I just want to know a way to get the first name and last name of the logged in user.

Thank you 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

            }

        });

    }
}
    
asked by anonymous 07.09.2015 / 01:21

1 answer

1

I was able to solve my problem, as stated above I'm using the linkedin API where there's a class called APIResponse, so I understand it already performs all the JSON handling.

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

        }

    });

}
    
08.09.2015 / 02:49