Class yii\authclient\clients\Facebook

Inheritanceyii\authclient\clients\Facebook » yii\authclient\OAuth2 » yii\authclient\BaseOAuth » yii\authclient\BaseClient » yii\base\Component
Implementsyii\authclient\ClientInterface
Available since version2.0

Facebook allows authentication via Facebook OAuth.

In order to use Facebook OAuth you must register your application at https://developers.facebook.com/apps.

Example application configuration:

'components' => [
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'facebook' => [
                'class' => 'yii\authclient\clients\Facebook',
                'clientId' => 'facebook_client_id',
                'clientSecret' => 'facebook_client_secret',
            ],
        ],
    ]
    // ...
]

See also:

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$apiBaseUrl string API base URL. yii\authclient\clients\Facebook
$attributeNames array List of attribute names, which should be requested from API to initialize user attributes. yii\authclient\clients\Facebook
$authUrl string Authorize URL. yii\authclient\clients\Facebook
$autoExchangeAccessToken boolean Whether to automatically upgrade short-live (2 hours) access token to long-live (60 days) one, after fetching it. yii\authclient\clients\Facebook
$autoRefreshAccessToken boolean Whether to automatically perform 'refresh access token' request on expired access token. yii\authclient\clients\Facebook
$clientAuthCodeUrl string URL endpoint for the client auth code generation. yii\authclient\clients\Facebook
$clientId string OAuth client ID. yii\authclient\OAuth2
$clientSecret string OAuth client secret. yii\authclient\OAuth2
$scope string Auth request scope. yii\authclient\clients\Facebook
$tokenUrl string Token request URL endpoint. yii\authclient\clients\Facebook
$validateAuthState boolean Whether to use and validate auth 'state' parameter in authentication flow. yii\authclient\OAuth2
$version string Protocol version. yii\authclient\BaseOAuth

Public Methods

Hide inherited methods

MethodDescriptionDefined By
api() Performs request to the OAuth API returning response data. yii\authclient\BaseOAuth
applyAccessTokenToRequest() Applies access token to the HTTP request instance. yii\authclient\clients\Facebook
authenticateClient() Authenticate OAuth client directly at the provider without third party (user) involved, using 'client_credentials' grant type. yii\authclient\OAuth2
authenticateUser() Authenticates user directly by 'username/password' pair, using 'password' grant type. yii\authclient\OAuth2
authenticateUserJwt() Authenticates user directly using JSON Web Token (JWT). yii\authclient\OAuth2
beforeApiRequestSend() Handles \yii\httpclient\Request::EVENT_BEFORE_SEND event. yii\authclient\BaseOAuth
buildAuthUrl() Composes user authorization URL. yii\authclient\OAuth2
createApiRequest() Creates an HTTP request for the API call. yii\authclient\BaseOAuth
createRequest() Creates HTTP request instance. yii\authclient\BaseClient
exchangeAccessToken() Exchanges short-live (2 hours) access token to long-live (60 days) one. yii\authclient\clients\Facebook
fetchAccessToken() Fetches access token from authorization code. yii\authclient\clients\Facebook
fetchClientAccessToken() Fetches access token from client-specific authorization code. yii\authclient\clients\Facebook
fetchClientAuthCode() Requests the authorization code for the client-specific access token. yii\authclient\clients\Facebook
getAccessToken() yii\authclient\BaseOAuth
getHttpClient() Returns HTTP client. yii\authclient\BaseClient
getId() yii\authclient\ClientInterface
getName() yii\authclient\ClientInterface
getNormalizeUserAttributeMap() yii\authclient\BaseClient
getRequestOptions() yii\authclient\BaseClient
getReturnUrl() yii\authclient\BaseOAuth
getSignatureMethod() yii\authclient\BaseOAuth
getStateStorage() yii\authclient\BaseClient
getTitle() yii\authclient\ClientInterface
getUserAttributes() yii\authclient\ClientInterface
getViewOptions() yii\authclient\ClientInterface
refreshAccessToken() Gets new auth token to replace expired one. yii\authclient\BaseOAuth
setAccessToken() Sets access token to be used. yii\authclient\BaseOAuth
setHttpClient() Sets HTTP client to be used. yii\authclient\BaseOAuth
setId() yii\authclient\ClientInterface
setName() yii\authclient\ClientInterface
setNormalizeUserAttributeMap() yii\authclient\BaseClient
setRequestOptions() yii\authclient\BaseClient
setReturnUrl() yii\authclient\BaseOAuth
setSignatureMethod() Set signature method to be used. yii\authclient\BaseOAuth
setStateStorage() yii\authclient\BaseClient
setTitle() yii\authclient\ClientInterface
setUserAttributes() yii\authclient\BaseClient
setViewOptions() yii\authclient\BaseClient

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
applyClientCredentialsToRequest() Applies client credentials (e.g. $clientId and $clientSecret) to the HTTP request instance. yii\authclient\OAuth2
composeUrl() Composes URL from base URL and GET params. yii\authclient\BaseOAuth
createHttpClient() Creates HTTP client instance from reference or configuration. yii\authclient\BaseOAuth
createSignatureMethod() Creates signature method instance from its configuration. yii\authclient\BaseOAuth
createToken() Creates token from its configuration. yii\authclient\BaseOAuth
defaultName() Generates service name. yii\authclient\clients\Facebook
defaultNormalizeUserAttributeMap() Returns the default \yii\authclient\normalizeUserAttributeMap value. yii\authclient\BaseClient
defaultRequestOptions() Returns default HTTP request options. yii\authclient\BaseOAuth
defaultReturnUrl() Composes default \yii\authclient\returnUrl value. yii\authclient\BaseOAuth
defaultTitle() Generates service title. yii\authclient\clients\Facebook
defaultViewOptions() Returns the default \yii\authclient\clients\viewOptions value. yii\authclient\clients\Facebook
generateAuthState() Generates the auth state value. yii\authclient\OAuth2
getState() Returns persistent state value. yii\authclient\BaseClient
getStateKeyPrefix() Returns session key prefix, which is used to store internal states. yii\authclient\BaseClient
initUserAttributes() Initializes authenticated user attributes. yii\authclient\clients\Facebook
normalizeUserAttributes() Normalize given user attributes according to \yii\authclient\normalizeUserAttributeMap. yii\authclient\BaseClient
removeState() Removes persistent state value. yii\authclient\BaseClient
restoreAccessToken() Restores access token. yii\authclient\BaseOAuth
saveAccessToken() Saves token as persistent state. yii\authclient\BaseOAuth
sendRequest() Sends the given HTTP request, returning response data. yii\authclient\BaseOAuth
setState() Sets persistent state. yii\authclient\BaseClient

Property Details

$apiBaseUrl public property

API base URL. This field will be used as \yii\httpclient\Client::baseUrl value of \yii\authclient\clients\httpClient. Note: changing this property will take no effect after \yii\authclient\clients\httpClient is instantiated.

public string $apiBaseUrl 'https://graph.facebook.com'
$attributeNames public property (available since version 2.0.5)

List of attribute names, which should be requested from API to initialize user attributes.

public array $attributeNames = ['name''email']
$authUrl public property

Authorize URL.

public string $authUrl 'https://www.facebook.com/dialog/oauth'
$autoExchangeAccessToken public property (available since version 2.1.3)

Whether to automatically upgrade short-live (2 hours) access token to long-live (60 days) one, after fetching it.

See also \yii\authclient\clients\exchangeToken().

$autoRefreshAccessToken public property

Whether to automatically perform 'refresh access token' request on expired access token.

$clientAuthCodeUrl public property (available since version 2.1.3)
public string $clientAuthCodeUrl 'https://graph.facebook.com/oauth/client_code'
$scope public property

Auth request scope.

public string $scope 'email'
$tokenUrl public property

Token request URL endpoint.

public string $tokenUrl 'https://graph.facebook.com/oauth/access_token'

Method Details

applyAccessTokenToRequest() public method

Applies access token to the HTTP request instance.

public void applyAccessTokenToRequest ( $request, $accessToken )
$request \yii\httpclient\Request

HTTP request instance.

$accessToken yii\authclient\OAuthToken

Access token instance.

defaultName() protected method

Generates service name.

protected string defaultName ( )
return string

Service name.

defaultTitle() protected method

Generates service title.

protected string defaultTitle ( )
return string

Service title.

defaultViewOptions() protected method

Returns the default \yii\authclient\clients\viewOptions value.

Particular client may override this method in order to provide specific default view options.

protected array defaultViewOptions ( )
return array

List of default \yii\authclient\clients\viewOptions

exchangeAccessToken() public method (available since version 2.1.3)

Exchanges short-live (2 hours) access token to long-live (60 days) one.

Note that this method will success for already long-live token, but will not actually prolong it any further. Pay attention, that this method will fail on already expired access token.

See also https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension.

public yii\authclient\OAuthToken exchangeAccessToken ( yii\authclient\OAuthToken $token )
$token yii\authclient\OAuthToken

Short-live access token.

return yii\authclient\OAuthToken

Long-live access token.

fetchAccessToken() public method

Fetches access token from authorization code.

public yii\authclient\OAuthToken fetchAccessToken ( $authCode, array $params = [] )
$authCode string

Authorization code, usually comes at GET parameter 'code'.

$params array

Additional request params.

return yii\authclient\OAuthToken

Access token.

throws \yii\web\HttpException

on invalid auth state in case \yii\authclient\clients\enableStateValidation is enabled.

fetchClientAccessToken() public method (available since version 2.1.3)

Fetches access token from client-specific authorization code.

This make sense for the distributed applications, which provides several Auth clients (web and mobile) to avoid triggering Facebook's automated spam systems.

See also:

public yii\authclient\OAuthToken fetchClientAccessToken ( $authCode, array $params = [] )
$authCode string

Client auth code.

$params array
return yii\authclient\OAuthToken

Long-live client-specific access token.

fetchClientAuthCode() public method (available since version 2.1.3)

Requests the authorization code for the client-specific access token.

This make sense for the distributed applications, which provides several Auth clients (web and mobile) to avoid triggering Facebook's automated spam systems.

See also:

public string fetchClientAuthCode ( yii\authclient\OAuthToken $token null, $params = [] )
$token yii\authclient\OAuthToken|null

Access token, if not set \yii\authclient\clients\accessToken will be used.

$params array

Additional request params.

return string

Client auth code.

initUserAttributes() protected method

Initializes authenticated user attributes.

protected array initUserAttributes ( )
return array

Auth user attributes.