NAME

Linkedin::OAuth2 - An easy way to authenticate users via LinkedIn.

VERSION

version 0.2

SYNOPSIS

my $linkedin = Linkedin::OAuth2->new( key => 'your-app-key', secret => 'your-app-secret');

get authorization url({ redirect_uri => 'http://localhost:3000/user/linkedin', scope => ['r_basicprofile','rw_groups','r_emailaddress']})

# scope is an array of permissions that your app requires, see http://developer.linkedin.com/documents/authentication#granting for more details, this field is optional

my $authorization_code_url = $linkedin->authorization_code_url( redirect_uri => 'url_of_your_app_to_intercept_success', scope => ['r_basicprofile','rw_groups','r_emailaddress'] );

# convert code response to an access token # redirect_uri is the url where you will check for the parameter code. # param('code') is the parameter 'code' that you will get after the user authorizes your app and gets redirected to the redirect_uri (callback) page.

my $token_object = $linkedin->get_access_token( authorization_code => param('code'), redirect_uri => 'your-app-redirect-url-or-callback' );

# use the new token to request user information

my $result = $linkedin->request( url => 'https://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url,email-address)?format=json', token => $token_object->{access_token} );

# we have the email address if ($result->{emailAddress}) { # ... }

# Or here is an entire login logic or recipe:

	my $linkedin = Linkedin::OAuth2->new( key => 'your-app-key',
	                                   secret => 'your-app-secret');
	
	
	# catch the code param and try to convert it into an access_token and get the email address
	if (param('code')) {

	    my $token_object = $linkedin->get_access_token(
		    authorization_code => param('code'),
		    # has to be the same redirect_uri you specified in the code before
		    redirect_uri =>       'your-app-redirect-uri-or-callback-url'
	    );
	    
	    my $result = $linkedin->request(
	    	url    => 'https://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url,email-address)?format=json',
		 	token  => $token_object->{access_token} );

		if ($result->{emailAddress}) {
			# we have the email address, authenticate the user and redirect somewhere..
			# ....
			
			return;
		} else {
			# we did not get an email address
			# redirect to try again?
			
			return;
		}
	    
	}
	
	# get the url for permissions
	
	my $authorization_code_url = $linkedin->authorization_code_url(
		# this field is required
	    redirect_uri => 'your-app-redirect', 
	    #array of permissions that your app requires, see http://developer.linkedin.com/documents/authentication#granting for more details, this field is optional
	    scope    => ['r_basicprofile','rw_groups','r_emailaddress'] 
	);
	
	#redirect the user to get their permission
	redirect($authorization_code_url);

	# and catch an error back from linked in
	if (param('error')) {
	    # handle the error
	    # if the user denied, redirect to try again...
	}
    

SEE ALSO

http://developer.linkedin.com/documents

AUTHOR

Asaf Klibansky

discobeta@gmail.com

METHODS

authorization_code_url( { redirect_uri => '...', scope => '...'} )

    Definition: This method is used to get the url required to authenticate the user via LinkedIn OAuth2. It assumes that you have a linkedin api key and secret which you may obtain here https://www.linkedin.com/secure/developer. Basically this builds the url where you should redirect the user to obtain their permission to access certain information (scope) on linkedin.

    Accepts: a hash or hashref of arguments. They must include the necessary information to build the url. redirect_url is a url where the user should be redirect to after successfuly authorizing (or not) and should be a method ready to capture the 'code' or 'error' parameters.' scope scope is an array of permissions that your app requires, see http://developer.linkedin.com/documents/authentication#granting for more details, this field is optional

    Returns: This will return a string containing the url where the user should be redirect to in order to obtain their linkedin permissions. =back

get_access_token( { authorization_code => '...', redirect_uri => '...'} )

    Definition: This method is used to convert the parameter 'code' that we got from facebook after obtaining the user permissions into an access token that we can later use to access the LinkedIn API.

    Accepts: a hash or hashref of arguments. They must include the necessary information to convert the code. authorization_code is the parameter linkedin provided you with after successfully obtaining a user's permission. redirect_uri is a url where the user should be redirect to after successfuly authorizing (or not) and should be a method ready to capture the 'code' or 'error' parameters.'

    Returns: This will return a hash containing an access_token and an expires_in keys and values

new( { key => '...', secret => '...', scope => ['...'] } )

    Definition: This method is used to convert the parameter 'code' that we got from facebook after obtaining the user permissions into an access token that we can later use to access the LinkedIn API.

    Accepts: a hash or hashref of arguments. They must include the necessary information to convert the code. key is the key linkedin provided you with when you create an app. See https://www.linkedin.com/secure/developer for more details. secret is tha app secret that linkedin provided you with when you create an app. scope scope is an array of permissions that your app requires, see http://developer.linkedin.com/documents/authentication#granting for more details, this field is optional

    Returns: This will create an interface to the linked in API

request( { url => '...', token => '...' } )

    Definition: This method is used to access the linkedin api.

    Accepts: a hash or hashref of arguments. They must include the necessary information to convert the code. url is the linkedin API url to access. See https://developer.linkedin.com/docs for more details. token is a valid token that you retrieved from a successful linkedin authentication.

    Returns: This will create an interface to the linked in API