Fórum

Authenticate user with LinkedIn

thumbnail
Anindya Mallik, modificado 8 Anos atrás.

Authenticate user with LinkedIn

Junior Member Postagens: 82 Data de Entrada: 15/01/13 Postagens Recentes
Hi All,

How to implement "Authenticate user with LinkedIn" at the time of registration? I know there is plugins in market place. But is there any way to implement it by coding?

I want something which I attached.

Please help.
thumbnail
Vishal Kumar, modificado 8 Anos atrás.

RE: Authenticate user with LinkedIn

Regular Member Postagens: 198 Data de Entrada: 12/12/12 Postagens Recentes
May this help you : https://developer.linkedin.com/docs/oauth2
thumbnail
Andew Jardine, modificado 8 Anos atrás.

RE: Authenticate user with LinkedIn

Liferay Legend Postagens: 2416 Data de Entrada: 22/12/10 Postagens Recentes
Hi Anindya,

I haven't done this with LinkedIn, but I have done this with Twitter and Google+. I imagine the changes that are required are pretty much the same. I'm going back into the memory vault as it was a while back now but here is what I remember.

1. We created a hook project and overrode the login.jsp (/html/portlet/login). This change included essentially copying and pasting the same logic used for the Facebook login so that there were two additional links (in our case -- twitter and g+).

2. These links used the Twitter and G+ apis (similar to what has been referenced below) to redirect the user to the OAuth pages for the providers. We made these settings all configurable in the portal-ext.properties and used the PrefsPropsUtil class to get them (using the methods that provide companyId so that we could scope it per portal instance).

3. The callback urls push the user to a specific location -- for example /c/portal/twitter_login. To handle this we created StrutsAction hooks. In our case it was a two phase authentication where we then checked for the user account in another system and then forwarded the user either into the site or to the registration page (to register an account that was linked to their social media account).

4. We didn't need to do this step bu tyou can also provide a *AutoLogin class so that if the OAuth tokens are present, they don't have to provide their login details again.

-- the best thing to do is have a look at the source code at how the out of the box Portal manages this process for LinkedIn. Replicate the classes and packages, but use the LinkedIn API in place of Facebooks.
thumbnail
Anindya Mallik, modificado 8 Anos atrás.

RE: Authenticate user with LinkedIn

Junior Member Postagens: 82 Data de Entrada: 15/01/13 Postagens Recentes
Hi Andew,

Many many thanks for your reply. Surely I will try what you have told, and I will let you know.

Now I have created a portlet where the users can authorize his/her LinkedIn account, but I stuck in one point.
I am referring this site

When I am trying to obtain an Access Token is for my application using the Authorization Code it just acquired. I am using this code

DefaultHttpClient client = new DefaultHttpClient();
URI uri = new URIBuilder().setScheme("https")
        .setHost("www.linkedin.com")
        .setPath("/uas/oauth2/accessToken")
        .setParameter("grant_type", "authorization_code")
        .setParameter("code", code)
        .setParameter("redirect_uri", "http://localhost:9090/ConnectSocialMedia/callBack.jsp")
        .setParameter("client_id", CONSUMER_KEY_OPTION)
        .setParameter("client_secret", CONSUMER_SECRET_OPTION)
        .build();
HttpPost post = new HttpPost(uri);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response2 = client.execute(post);
System.out.println("Final Response------------>"+response2);


But I am getting HTTP/1.1 400 Bad Request. What am I doing wrong?

Any help would be really appreciated!

Note: The code I have given is not a liferay code, Its a J2EE web application, after getting success I will implement it in Liferay.
thumbnail
Anindya Mallik, modificado 8 Anos atrás.

RE: Authenticate user with LinkedIn

Junior Member Postagens: 82 Data de Entrada: 15/01/13 Postagens Recentes
Yes, I was doing wrong as I was passing parameters in a wrong way. Here is the solved code


DefaultHttpClient client = new DefaultHttpClient();
DefaultHttpClient client2 = new DefaultHttpClient();
URI uri = new URIBuilder().setScheme("https")
.setHost("www.linkedin.com")
.setPath("/uas/oauth2/accessToken")
.build();
	
List <namevaluepair> nvps = new ArrayList <namevaluepair>();
nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
nvps.add(new BasicNameValuePair("code", code));
nvps.add(new BasicNameValuePair("redirect_uri", "http://192.168.1.125:9090/ConnectSocialMedia/callBack.jsp"));
nvps.add(new BasicNameValuePair("client_id", CONSUMER_KEY));
nvps.add(new BasicNameValuePair("client_secret", CONSUMER_KEY_SECRET));
	
HttpPost post = new HttpPost(uri);
post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

System.out.println("Final Post----------------&gt;"+post.getURI());
HttpResponse response2 = client.execute(post);
System.out.println("Final Response------------&gt;"+response2);</namevaluepair></namevaluepair>
thumbnail
Vishal Kumar, modificado 8 Anos atrás.

RE: Authenticate user with LinkedIn

Regular Member Postagens: 198 Data de Entrada: 12/12/12 Postagens Recentes
Thanks. emoticon
thumbnail
Andew Jardine, modificado 8 Anos atrás.

RE: Authenticate user with LinkedIn

Liferay Legend Postagens: 2416 Data de Entrada: 22/12/10 Postagens Recentes
Hi Anindya,

Glad to hear you got it working. For any future passers by, once thing I forgot to mention was that when I was integrating with Google+ and Twitter I came across a library that made my life a lot simpler. I'm not sure if you used it as well, but in either case it was called scribe and you can find the github project here == https://github.com/fernandezpablo85/scribe-java