Use LinkedIn v2 API (#1890)

This commit is contained in:
Clément OUDOT 2019-08-22 14:51:24 +02:00
parent 894b8be541
commit e42e71b5e3

View File

@ -8,7 +8,7 @@ use Lemonldap::NG::Common::FormEncode;
use Lemonldap::NG::Common::UserAgent; use Lemonldap::NG::Common::UserAgent;
use Lemonldap::NG::Portal::Main::Constants qw(PE_OK PE_ERROR PE_REDIRECT); use Lemonldap::NG::Portal::Main::Constants qw(PE_OK PE_ERROR PE_REDIRECT);
our $VERSION = '2.0.0'; our $VERSION = '2.0.6';
extends 'Lemonldap::NG::Portal::Main::Auth'; extends 'Lemonldap::NG::Portal::Main::Auth';
@ -50,7 +50,16 @@ has linkedInPeopleEndpoint => (
lazy => 1, lazy => 1,
default => sub { default => sub {
$_[0]->conf->{linkedInPeopleEndpoint} $_[0]->conf->{linkedInPeopleEndpoint}
|| 'https://api.linkedin.com/v1/people/'; || 'https://api.linkedin.com/v2/me';
}
);
has linkedInEmailEndpoint => (
is => 'ro',
lazy => 1,
default => sub {
$_[0]->conf->{linkedInEmailEndpoint}
|| 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))';
} }
); );
@ -123,13 +132,11 @@ sub extractFormInfo {
$self->logger->debug("Get access token $access_token from LinkedIn"); $self->logger->debug("Get access token $access_token from LinkedIn");
# Build People EndPoint URI # Call People EndPoint URI
my $linkedInPeopleEndpoint = $self->logger->debug(
$self->linkedInPeopleEndpoint . "~:(" "Call LinkedIn People Endpoint " . $self->linkedInPeopleEndpoint );
. $self->conf->{linkedInFields}
. ")?format=json";
my $people_response = $self->ua->get( $linkedInPeopleEndpoint, my $people_response = $self->ua->get( $self->linkedInPeopleEndpoint,
"Authorization" => "Bearer $access_token" ); "Authorization" => "Bearer $access_token" );
if ( $people_response->is_error ) { if ( $people_response->is_error ) {
@ -141,6 +148,9 @@ sub extractFormInfo {
my $people_content = $people_response->decoded_content; my $people_content = $people_response->decoded_content;
$self->logger->debug(
"Response from LinkedIn People API: $people_content");
eval { eval {
$json_hash = from_json( $people_content, { allow_nonref => 1 } ); $json_hash = from_json( $people_content, { allow_nonref => 1 } );
}; };
@ -153,6 +163,39 @@ sub extractFormInfo {
$req->data->{linkedInData}->{$_} = $json_hash->{$_}; $req->data->{linkedInData}->{$_} = $json_hash->{$_};
} }
# Call Email EndPoint URI
if ( $self->conf->{linkedInScope} =~ /r_emailaddress/ ) {
$self->logger->debug( "Call LinkedIn Email Endpoint "
. $self->linkedInEmailEndpoint );
my $email_response = $self->ua->get( $self->linkedInEmailEndpoint,
"Authorization" => "Bearer $access_token" );
if ( $email_response->is_error ) {
$self->logger->error(
"Bad authorization response: " . $email_response->message );
$self->logger->debug( $email_response->content );
return PE_ERROR;
}
my $email_content = $email_response->decoded_content;
$self->logger->debug(
"Response from LinkedIn Email API: $email_content");
eval {
$json_hash = from_json( $email_content, { allow_nonref => 1 } );
};
if ($@) {
$self->logger->error("Unable to decode JSON $email_content");
return PE_ERROR;
}
$req->data->{linkedInData}->{"emailAddress"} =
$json_hash->{"elements"}->[0]{"handle~"}->{"emailAddress"};
}
$req->user( $req->user(
$req->data->{linkedInData}->{ $self->conf->{linkedInUserField} } ); $req->data->{linkedInData}->{ $self->conf->{linkedInUserField} } );