Ejemplo n.º 1
0
$client = new OAuthClient($settings['global']['clientId'], $settings['global']['clientSecret']);
if (isset($_COOKIE['yaAccessToken'])) {
    $client->setAccessToken($_COOKIE['yaAccessToken']);
    echo '<p>PHP: Access token from cookies is ' . htmlentities($client->getAccessToken()) . '</p>';
}
/**
 * There are two ways to get an access token from Yandex OAuth API.
 * First one is to get in browser after # symbol. It's handled above with JS and PHP.
 * Second one is to use an intermediate code. It's handled below with PHP.
 *
 * This file implements both cases because the only one callback url can be set for an application.
 *
 */
if (isset($_REQUEST['code'])) {
    try {
        $client->requestAccessToken($_REQUEST['code']);
    } catch (\Yandex\OAuth\Exception\AuthRequestException $ex) {
        echo '<p class="text-danger">' . $ex->getMessage() . '</p>';
    }
    if ($client->getAccessToken()) {
        echo "<p>PHP: Access token from server is " . $client->getAccessToken() . '</p>';
        if (isset($_GET['state']) && $_GET['state']) {
            echo '<p>PHP: State is "' . htmlentities($_GET['state']) . '"</p>';
        }
        setcookie('yaAccessToken', $client->getAccessToken(), 0, '/');
        setcookie('yaClientId', $settings['global']['clientId'], 0, '/');
    }
} elseif (isset($_GET['error'])) {
    echo '<p>PHP: Server redirected with error "' . htmlentities($_GET['error']) . '"';
    if (isset($_GET['error_description'])) {
        echo ' and message "' . htmlentities($_GET['error_description']) . '"';