|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Example of retrieving an authentication token of the BattleNet service |
| 5 | + * |
| 6 | + * PHP version 5.4 |
| 7 | + * |
| 8 | + * @author And <and.webdev@gmail.com> |
| 9 | + * @copyright Copyright (c) 2015 The authors |
| 10 | + * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 11 | + */ |
| 12 | + |
| 13 | +use OAuth\Common\Consumer\Credentials; |
| 14 | +use OAuth\Common\Storage\Session; |
| 15 | +use OAuth\OAuth2\Service\BattleNet; |
| 16 | + |
| 17 | +/** |
| 18 | + * Bootstrap the example |
| 19 | + */ |
| 20 | +require_once __DIR__ . '/bootstrap.php'; |
| 21 | + |
| 22 | +// Session storage |
| 23 | +$storage = new Session(); |
| 24 | + |
| 25 | +// Setup the credentials for the requests |
| 26 | +$credentials = new Credentials( |
| 27 | + $servicesCredentials[ 'battlenet' ][ 'key' ], |
| 28 | + $servicesCredentials[ 'battlenet' ][ 'secret' ], |
| 29 | + $currentUri |
| 30 | +); |
| 31 | + |
| 32 | +// Instantiate the BattleNet service using the credentials, http client, storage mechanism for the token and profile scope |
| 33 | +/** @var BattleNet $battleNetService */ |
| 34 | +$battleNetService = $serviceFactory->createService( |
| 35 | + 'battlenet', |
| 36 | + $credentials, |
| 37 | + $storage, |
| 38 | + [BattleNet::SCOPE_SC2_PROFILE, BattleNet::SCOPE_WOW_PROFILE] |
| 39 | +); |
| 40 | +$battleNetService->setRegion('us'); |
| 41 | + |
| 42 | +if ($battleNetService->isGlobalRequestArgumentsPassed()) { |
| 43 | + // Retrieve a token and send a request |
| 44 | + $result = $battleNetService->retrieveAccessTokenByGlobReqArgs()->requestJSON('/account/user'); |
| 45 | + |
| 46 | + // Show some of the resultant data |
| 47 | + echo 'Your unique Battle.Net user id is: ' . $result[ 'id' ]; |
| 48 | +} elseif (!empty($_GET[ 'go' ]) && $_GET[ 'go' ] === 'go') { |
| 49 | + $battleNetService->redirectToAuthorizationUri(); |
| 50 | +} else { |
| 51 | + echo "<a href='$currentUri?go=go'>Login with Battle.Net!</a>"; |
| 52 | +} |
0 commit comments