You are here

protected function OpenIDConnectLinkedinClient::retrieveUserEmail in OpenID Connect / OAuth client 8

Same name and namespace in other branches
  1. 2.x src/Plugin/OpenIDConnectClient/OpenIDConnectLinkedinClient.php \Drupal\openid_connect\Plugin\OpenIDConnectClient\OpenIDConnectLinkedinClient::retrieveUserEmail()

Get user email.

Parameters

string $access_token: An access token string.

Return value

string|bool An email or false.

1 call to OpenIDConnectLinkedinClient::retrieveUserEmail()
OpenIDConnectLinkedinClient::retrieveUserInfo in src/Plugin/OpenIDConnectClient/OpenIDConnectLinkedinClient.php
Retrieves user info: additional user profile data.

File

src/Plugin/OpenIDConnectClient/OpenIDConnectLinkedinClient.php, line 107

Class

OpenIDConnectLinkedinClient
LinkedIn OpenID Connect client.

Namespace

Drupal\openid_connect\Plugin\OpenIDConnectClient

Code

protected function retrieveUserEmail($access_token) {
  $request_options = [
    'headers' => [
      'Authorization' => 'Bearer ' . $access_token,
      'Accept' => 'application/json',
    ],
  ];
  $endpoints = $this
    ->getEndpoints();
  try {
    $response = $this->httpClient
      ->get($endpoints['useremail'], $request_options);
    $object = json_decode((string) $response
      ->getBody(), TRUE);
    if (isset($object['elements'])) {
      foreach ($object['elements'] as $element) {
        if (isset($element['handle~']['emailAddress'])) {

          // The email address was found.
          return $element['handle~']['emailAddress'];
        }
      }
    }
  } catch (\Exception $e) {
    $variables = [
      '@message' => 'Could not retrieve user email information',
      '@error_message' => $e
        ->getMessage(),
    ];
    $this->loggerFactory
      ->get('openid_connect_' . $this->pluginId)
      ->error('@message. Details: @error_message', $variables);
  }

  // No email address was provided.
  return FALSE;
}