Exemple #1
0
 * Réglages:
 * - $DEV=true pour utiliser le serveur de test api06.dev.openstreetmap.org
 * - $DEV=false pour le serveur de prod www.openstreetmap.org
 *
 * Test en 2 phases.
 * 1. supprimé le fichier "*.token" (eg. tests/test_OSM_Auth_OAuth.php.token)
 * 2. lancer tests/test_OSM_Auth_OAuth.php et suivre les instructions
 * 3. relancer tests/test_OSM_Auth_OAuth.php qui va réutiliser le token obtenu (stocké dans "*.token" )
 * 		en étape 2 pour accéder aux données de l'utilisateur.
 *
 * Note: files "secrets.php" and "test_OSM_OAuth.php.token" are ignored by Git (see .gitignore)
 */
$DEV = true;
$time_start = microtime(true);
require_once __DIR__ . '/tests_common.php';
_wl('test "' . basename(__FILE__) . '');
require_once __DIR__ . '/../lib/OSM/Api.php';
include_once __DIR__ . '/secrets.php';
if ($DEV) {
    $apiUrl = 'http://api06.dev.openstreetmap.org/api/0.6';
    $oauth = new OSM_Auth_OAuth($AUTH_OAUTH_CONSUMER_KEY_DEV, $AUTH_OAUTH_CONSUMER_SECRET_DEV, array('requestTokenUrl' => OSM_Auth_OAuth::REQUEST_TOKEN_URL_DEV, 'accessTokenUrl' => OSM_Auth_OAuth::ACCESS_TOKEN_URL_DEV, 'authorizeUrl' => OSM_Auth_OAuth::AUTHORIZE_TOKEN_URL_DEV));
} else {
    $apiUrl = 'http://www.openstreetmap.org/api/0.6';
    $oauth = new OSM_Auth_OAuth($AUTH_OAUTH_CONSUMER_KEY_PROD, $AUTH_OAUTH_CONSUMER_SECRET_PROD);
}
$tokenFilename = __DIR__ . '/' . basename(__FILE__) . '.token';
if (file_exists($tokenFilename)) {
    echo 'Reusing Authorization...' . "\n";
    $fp = fopen($tokenFilename, 'r');
    eval('$authCredentials = ' . file_get_contents($tokenFilename) . ';');
    fclose($fp);
    if ($auth_method == 'OAuth') {
        _wl(' using OAuth auth with consumerKey="' . $auth_oauth_consumer_key . '"');
        $oauth = new OSM_Auth_OAuth($auth_oauth_consumer_key, $auth_oauth_consumer_secret);
        $oauth->setAccessToken($auth_oauth_token, $auth_oauth_secret);
        $osmApi->setCredentials($oauth);
    }
}
// http://api06.dev.openstreetmap.org/api/0.6/relation/500
// http://api06.dev.openstreetmap.org/api/0.6/way/8184
// http://api06.dev.openstreetmap.org/api/0.6/node/611571
// get a node
$permissions = $osmApi->getAuthPermissions();
echo print_r($permissions, true) . "\n";
if ($auth_method == 'Basic') {
    _assert($osmApi->isAllowedToReadPrefs() === true);
    _assert($osmApi->isAllowedToWritePrefs() === true);
    _assert($osmApi->isAllowedToWriteDiary() === true);
    _assert($osmApi->isAllowedToWriteApi() === true);
    _assert($osmApi->isAllowedToReadGpx() === true);
    _assert($osmApi->isAllowedToWriteGpx() === true);
} else {
    _assert($osmApi->isAllowedToReadPrefs() === true);
    _assert($osmApi->isAllowedToWritePrefs() === true);
    _assert($osmApi->isAllowedToWriteDiary() === false);
    _assert($osmApi->isAllowedToWriteApi() === true);
    _assert($osmApi->isAllowedToReadGpx() === true);
    _assert($osmApi->isAllowedToWriteGpx() === false);
}
$time_end = microtime(true);
_wl('Test well done in ' . number_format($time_end - $time_start, 3) . ' second(s).');
Exemple #3
0
    if ($creds['token'] == $_REQUEST['oauth_token']) {
        $oauth->requestAccessToken(isset($_REQUEST['oauth_verifier']) ? $_REQUEST['oauth_verifier'] : null);
    } else {
        echo '<p>ERROR, oauth token does not match !</p>' . "\n";
    }
}
if (isset($_REQUEST['go'])) {
    if (!$oauth->hasAccessToken()) {
        try {
            // try to get a access token
            $oauth->requestAccessToken();
        } catch (OSM_HttpException $ex) {
            _wl('Could not get access. http:' . $ex->getHttpCode());
            // if it fails,
            if ($ex->getHttpCode() == '401') {
                _wl('Request access authorization');
                $api->clearCachedAuthPermissions();
                $req = $oauth->requestAuthorizationUrl();
                //$oauth->setToken( $req["token"], $req["tokenSecret"] );
                header("Location:" . $req["url"]);
                exit;
            }
        }
    }
}
function _wl($s)
{
    global $applicationName, $_wlCounter;
    if (!isset($_wlCounter)) {
        $_wlCounter = 0;
    }