You are here

public function OpenIDConnectFacebookClient::retrieveUserInfo in OpenID Connect / OAuth client 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/OpenIDConnectClient/OpenIDConnectFacebookClient.php \Drupal\openid_connect\Plugin\OpenIDConnectClient\OpenIDConnectFacebookClient::retrieveUserInfo()

Retrieves user info: additional user profile data.

Parameters

string $access_token: Access token.

Return value

array|null Additional user profile information or NULL on failure.

Overrides OpenIDConnectClientBase::retrieveUserInfo

File

src/Plugin/OpenIDConnectClient/OpenIDConnectFacebookClient.php, line 91

Class

OpenIDConnectFacebookClient
Facebook OpenID Connect client.

Namespace

Drupal\openid_connect\Plugin\OpenIDConnectClient

Code

public function retrieveUserInfo(string $access_token) : ?array {
  $request_options = [
    'query' => [
      'access_token' => $access_token,
      'fields' => implode(',', $this->fields),
    ],
    'headers' => [
      'Accept' => 'application/json',
    ],
  ];
  $endpoints = $this
    ->getEndpoints();

  /** @var \GuzzleHttp\Client $client */
  $client = $this->httpClient;
  try {
    $response = $client
      ->get($endpoints['userinfo'], $request_options);
    $userinfo = Json::decode((string) $response
      ->getBody());

    // Make sure the result is an array before returning it.
    if (is_array($userinfo)) {
      $userinfo['sub'] = $userinfo['id'];
      if (!empty($userinfo['picture']['data']['url'])) {
        $userinfo['picture'] = $userinfo['picture']['data']['url'];
      }
      return $userinfo;
    }
  } catch (\Exception $e) {
    $variables = [
      '@message' => 'Could not retrieve user profile information',
      '@error_message' => $e
        ->getMessage(),
    ];
    $this->loggerFactory
      ->get('openid_connect_' . $this->pluginId)
      ->error('@message. Details: @error_message', $variables);
  }
  return NULL;
}