Hi,
Today I want to share about how to get linkedin profile details into in your application(asp.net app) using linkedin OAuth2.0 API.
Here I will create one simple asp.net application and I will show how to get LinkedIn profile and display in a page.
I. Register an application on LinkedIn
To get LinkedIn profile details we need to generate/create one profile in https://developers.linkedin.com. once you create the profile it will generate one id and one secret key.
Using these two keys in your application we can get the users profile details
Here the steps to create profile and generate keys
1. Go to " https://developer.linkedin.com" then click on " My Apps" on top navigation.
2. Once you go to My Apps page its looks like as below and click on "Add new application"
3. Now enter all required information. we are creating this application in development environment so we use localhost and we use one static port number. when you are creating profile for production environment then give host url here.
4. Once you successfully created your profile, it automatically generate API Key and Secret Key
Now we will create development web application in asp.net and try to get user's profile details.
II. Get an access token -
Here I want to give a overview on how the application works.
OAuth2 basically allows third party application, to access protected resource of a specific user. You may have seen some web applications/sites allows you to login through your Google, Facebook, LinkedIn etc accounts. As an example just try this with any mentioned account (Google, Facebook, Yahoo etc.). After giving your username password credentials you will be notified the authorities which the third party application will get if you agreed to proceed.
First we need to authenticate the application with API Key and Secret Key.
Once it is authenticated, likedin API will ask for user credentials.
If user enter correct username and password it will generate code and that code will sent back to the redirect URL.
Using the code in application will request the access token with HTTP POST request method.
if the code is validate/verified, it will generate/returns access token key with expire time.
With this access token key we can get user profile details by passing access key token in the http header request with HTTP GET request method.
The access token represent the credentials to access protected resources. So your application does not need to store resource owner's credentials, but the access token.
Here the steps to create application to get user profile details
1. Create one asp.net application using Visual Studio 2012 named it as "GetLinkedInUserProfileDetails".
2. Make sure that application run on the port number which you have given while creating profile in developer site.
3. Now we need to add LinkedIn profile API Key and Secret Key by using one class file call it as "LinkedinAuthDetails" and the class looks like as below.
4. Now design your master page and add login.aspx with one button as shown below.
when user clicked on the button, it will go to LinkedIn for application authorization.
Here the code in button click event.
Response.Redirect("https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=" + obj.apiKey + "&scope=r_fullprofile&state=DCEEFWF45453sdffef424&redirect_uri=" + HttpUtility.HtmlEncode(obj.redirectUrl));
If the query string have correct API Key, and website URL then it will ask for user credentials.
If not it will throw an error and out put will be like this.
Once the credentials are validated then it will redirect to "RedirectUrl" which is the query string along with the code value. This code value is used to get access token for the loggedin user profile.
After successful user credential validation it is redirected to "ProfileDetails" page with code value in query string.
5. In ProfileDetails page, we have code value with this code value we will try to get access token key by using http web request with POST method.
dynamic responseText = obj.VerifyAuthentication(Request.QueryString["code"]);
This is the method to get access key token.
Here the method definition.
III. Make API calls - Yes, after obtaining access token you are free to call API methods with that, until it expires.
Now we will make HTTP web request to LinkedIn API for user profile details with GET method.
string authcode = "";
dynamic dynobj = JsonConvert.DeserializeObject(responseText);
foreach (var item in dynobj)
{
if (item.Name == "access_token")
{
authcode = "Bearer " + item.Value; break;
}
}
response = GetProfileResponse(authcode);
If the HTTP request header has valid access token then it will give response with user profile details.
There the application output.
Click here for source code.
Today I want to share about how to get linkedin profile details into in your application(asp.net app) using linkedin OAuth2.0 API.
Here I will create one simple asp.net application and I will show how to get LinkedIn profile and display in a page.
I. Register an application on LinkedIn
To get LinkedIn profile details we need to generate/create one profile in https://developers.linkedin.com. once you create the profile it will generate one id and one secret key.
Using these two keys in your application we can get the users profile details
Here the steps to create profile and generate keys
1. Go to " https://developer.linkedin.com" then click on " My Apps" on top navigation.
2. Once you go to My Apps page its looks like as below and click on "Add new application"
3. Now enter all required information. we are creating this application in development environment so we use localhost and we use one static port number. when you are creating profile for production environment then give host url here.
4. Once you successfully created your profile, it automatically generate API Key and Secret Key
Now we will create development web application in asp.net and try to get user's profile details.
II. Get an access token -
Here I want to give a overview on how the application works.
OAuth2 basically allows third party application, to access protected resource of a specific user. You may have seen some web applications/sites allows you to login through your Google, Facebook, LinkedIn etc accounts. As an example just try this with any mentioned account (Google, Facebook, Yahoo etc.). After giving your username password credentials you will be notified the authorities which the third party application will get if you agreed to proceed.
First we need to authenticate the application with API Key and Secret Key.
Once it is authenticated, likedin API will ask for user credentials.
If user enter correct username and password it will generate code and that code will sent back to the redirect URL.
Using the code in application will request the access token with HTTP POST request method.
if the code is validate/verified, it will generate/returns access token key with expire time.
With this access token key we can get user profile details by passing access key token in the http header request with HTTP GET request method.
The access token represent the credentials to access protected resources. So your application does not need to store resource owner's credentials, but the access token.
Here the steps to create application to get user profile details
1. Create one asp.net application using Visual Studio 2012 named it as "GetLinkedInUserProfileDetails".
2. Make sure that application run on the port number which you have given while creating profile in developer site.
3. Now we need to add LinkedIn profile API Key and Secret Key by using one class file call it as "LinkedinAuthDetails" and the class looks like as below.
when user clicked on the button, it will go to LinkedIn for application authorization.
Here the code in button click event.
Response.Redirect("https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=" + obj.apiKey + "&scope=r_fullprofile&state=DCEEFWF45453sdffef424&redirect_uri=" + HttpUtility.HtmlEncode(obj.redirectUrl));
If the query string have correct API Key, and website URL then it will ask for user credentials.
If not it will throw an error and out put will be like this.
Once the credentials are validated then it will redirect to "RedirectUrl" which is the query string along with the code value. This code value is used to get access token for the loggedin user profile.
After successful user credential validation it is redirected to "ProfileDetails" page with code value in query string.
5. In ProfileDetails page, we have code value with this code value we will try to get access token key by using http web request with POST method.
dynamic responseText = obj.VerifyAuthentication(Request.QueryString["code"]);
This is the method to get access key token.
Here the method definition.
III. Make API calls - Yes, after obtaining access token you are free to call API methods with that, until it expires.
Now we will make HTTP web request to LinkedIn API for user profile details with GET method.
string authcode = "";
dynamic dynobj = JsonConvert.DeserializeObject(responseText);
foreach (var item in dynobj)
{
if (item.Name == "access_token")
{
authcode = "Bearer " + item.Value; break;
}
}
response = GetProfileResponse(authcode);
Here the GetProfileResponse method definition.
There the application output.
Click here for source code.
No comments:
Post a Comment