4
votes

I'm trying to get the connections in linkedIn using their API, but when I try to retrieve the connections I get a 401 unauthorized error.

in the official documentation says

You must use an access token to make an authenticated call on behalf of a user

Make the API calls You can now use this access_token to make API calls on behalf of this user by appending "oauth2_access_token=access_token" at the end of the API call that you wish to make.

The API call that I'm trying to do is the following

Error --> http://api.linkedin.com/v1/people/~/connections:(id,headline,first-name,last-name)?format=json&oauth2_access_token=access_token

I have tried to do it with the following endpoint without any problems.

OK --> https://api.linkedin.com/v1/people/~:(id,first-name,last-name,formatted-name,date-of-birth,industry,email-address,location,headline,picture-urls::(original))?format=json&oauth2_access_token=access_token

this list of endpoints for the connections API are described here http://developer.linkedin.com/documents/connections-api I just copied and pasted one endpoint from there, so the question is what's the problem with the endpoint for getting the connections? what am I missing?

EDIT: for the preAuth Url I'm using

https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=ConsumerKey&scope=r_fullprofile%20r_emailaddress%20r_network&state&state=NewGuid&redirect_uri=Encoded_Url

https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=QueryString_Code&redirect_uri=EncodedCallback&client_id=ConsummerKey&client_secret=ConsumerSecret

please find attached the login screen requesting the permissions

enter image description here

EDIT2: Switched to https and worked like a charm!

2
switched to HTTPS and got rid of this error as wellPierre Lacave

2 Answers

4
votes

Access tokens are issued for a specific scope that describes the extent of the permission you are requesting. When you start the authentication transaction, you add a specific parameter (called scope) that requests the user to consent access to what you want (in this case their connections). If I remember correctly, in LinkedIn that is r_network.

Check their documentation here: http://developer.linkedin.com/documents/authentication#granting

So, your call is perfectly ok, but very likely your access_token doesn't have enough privileges.

2
votes
apiHelper.getRequest(getActivity(),"https://api.linkedin.com/v1/people/~/connections?modified=new", new ApiListener() {
        @Override
        public void onApiSuccess(ApiResponse response) {
        }

        @Override
        public void onApiError(LIApiError error) {
        }
    });

If you are trying to get user connections using the LinkedIn SDK for android like in the snippet above,

Check for permissions in the SDK in this class com.linkedin.platform.utils.Scope.

Make sure r_network is available when building your scope. Example

    public static final LIPermission R_NETWORK = new LIPermission("r_network", "Your network");

Can now be used like this to build scope

Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS, Scope.W_SHARE, Scope.R_FULLPROFILE, Scope.R_CONTACTINFO, Scope.R_NETWORK)