1. Go to this page and download the library: Download surfoo/oauth2-geocaching library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
surfoo / oauth2-geocaching example snippets
use League\OAuth2\Client\Provider\Geocaching;
$provider = new Geocaching([
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
'redirectUri' => 'https://your-app.com/callback',
'environment' => 'production', // 'dev', 'staging', 'production'
]);
// Get authorization URL
$authUrl = $provider->getAuthorizationUrl(['scope' => '*']);
$_SESSION['oauth2state'] = $provider->getState();
// Redirect user to Geocaching
header('Location: ' . $authUrl);
exit;
// In your callback handler
if (!empty($_GET['code'])) {
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$user = $provider->getResourceOwner($token);
echo 'Hello ' . $user->getUsername() . '!';
}
$provider->setResourceOwnerFields([
'referenceCode',
'username',
'findCount',
'hideCount',
'favoritePoints',
'membershipLevelId',
'joinedDateUtc',
'avatarUrl',
'profileText'
// Add any other fields supported by the Geocaching API
]);
$user = $provider->getResourceOwner($token);
// Now only the specified fields will be requested from the API
use Http\Client\Common\PluginClientFactory;
use League\OAuth2\Client\Plugin\TokenRefreshPlugin;
use League\OAuth2\Client\Provider\Geocaching;
use League\OAuth2\Client\Token\TokenStorageInterface;
use League\OAuth2\Client\Token\TokenSet;
use Nyholm\Psr7\Request;
use Psr\Log\NullLogger;
$provider = new Geocaching([
'clientId' => 'client_id',
'clientSecret' => 'client_secret',
'redirectUri' => 'https://your-app.test/callback',
'environment' => 'production', // or staging/dev
]);
$storage = new class implements TokenStorageInterface {
private ?TokenSet $tokens = null;
public function getTokens(string $referenceCode): ?TokenSet { return $this->tokens; }
public function saveTokens(string $referenceCode, TokenSet $tokens): void { $this->tokens = $tokens; }
public function lockUser(string $referenceCode, int $timeoutSeconds = 30): bool { return true; }
public function unlockUser(string $referenceCode): void {}
public function isUserLocked(string $referenceCode): bool { return false; }
};
$refreshPlugin = new TokenRefreshPlugin(
referenceCode: 'PR12345',
storage: $storage,
oauthProvider: $provider,
logger: new NullLogger(),
maxRetryAttempts: 3
);
$httpClient = (new PluginClientFactory())->createClient(
\Http\Discovery\Psr18ClientDiscovery::find(),
[$refreshPlugin]
);
$request = new Request('GET', $provider->apiDomain . '/v1/users/PR12345');
$response = $httpClient->sendRequest($request);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.