PHP code example of hytale-community / hytale-auth-php

1. Go to this page and download the library: Download hytale-community/hytale-auth-php 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/ */

    

hytale-community / hytale-auth-php example snippets


use HytaleCommunity\HytaleAuth\Configuration;

$configuration = new Configuration(
    clientId: 'your-client-id',
    redirectUri: 'https://example.com/auth/hytale/callback',
);

$configuration = new Configuration(
    clientId: 'your-client-id',
    redirectUri: 'https://example.com/auth/hytale/callback',
    clientSecret: 'your-client-secret',
);

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use HytaleCommunity\HytaleAuth\HytaleClient;

$httpClient = new Client();
$httpFactory = new HttpFactory();

$hytale = new HytaleClient(
    configuration: $configuration,
    httpClient: $httpClient,
    requestFactory: $httpFactory,
    streamFactory: $httpFactory,
);

use HytaleCommunity\HytaleAuth\Scope;

$authorization = $hytale->createAuthorizationRequest([
    Scope::Profile,
    Scope::GameOwnership,
]);

$authorization->state;
$authorization->nonce;
$authorization->codeVerifier;

$authorization->url;

$result = $hytale->authenticate(
    parameters: $_GET,
    expectedState: $_SESSION['hytale_state'],
    codeVerifier: $_SESSION['hytale_code_verifier'],
    expectedNonce: $_SESSION['hytale_nonce'],
);

$result->claims->subject;
$result->claims->profileUuid;
$result->claims->profileUsername;
$result->claims->sharedSource;
$result->claims->gameOwnership;
$result->claims->parentalManaged;

$result->tokens->accessToken;
$result->tokens->idToken;
$result->tokens->expiresIn;
$result->tokens->refreshToken;
$result->tokens->tokenType;

$result->claims->subject;

$result->claims->profileUuid;

$userInfo = $hytale->userInfo(
    $result->tokens->accessToken,
);

$userInfo->subject;
$userInfo->profileUuid;
$userInfo->profileUsername;
$userInfo->sharedSource;
$userInfo->gameOwnership;
$userInfo->parentalManaged;

$newTokens = $hytale->refresh($storedRefreshToken);

$storedRefreshToken = $newTokens->refreshToken;

$hytale->revoke($refreshToken);

use HytaleCommunity\HytaleAuth\Exception\OAuthException;

try {
    $result = $hytale->authenticate(
        parameters: $_GET,
        expectedState: $state,
        codeVerifier: $codeVerifier,
        expectedNonce: $nonce,
    );
} catch (OAuthException $exception) {
    $exception->error;
    $exception->errorDescription;
}

use HytaleCommunity\HytaleAuth\Exception\UnexpectedResponseException;
text
access_denied
invalid_scope
invalid_request
invalid_grant
invalid_client
unauthorized_client
bash
composer