You are here

public function OpenIDConnectFacebookClient::retrieveUserInfo in OpenID Connect / OAuth client 8

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

Implements OpenIDConnectClientInterface::retrieveUserInfo().

Parameters

string $access_token: An access token string.

Return value

array|bool A result array or false.

Overrides OpenIDConnectClientBase::retrieveUserInfo

File

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

Class

OpenIDConnectFacebookClient
Facebook OpenID Connect client.

Namespace

Drupal\openid_connect\Plugin\OpenIDConnectClient

Code

public function retrieveUserInfo($access_token) {
  $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);
    $response_data = (string) $response
      ->getBody();
    $userinfo = json_decode($response_data, TRUE);
    $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 FALSE;
  }
}