carlos-mg89 /
PHPoAuthLib
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Example of retrieving an authentication token of the Nest service. |
||
| 5 | * |
||
| 6 | * @author Pedro Amorim <[email protected]> |
||
| 7 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
| 8 | * |
||
| 9 | * @see https://developer.nest.com/documentation |
||
| 10 | */ |
||
| 11 | |||
| 12 | use OAuth\Common\Consumer\Credentials; |
||
| 13 | use OAuth\Common\Http\Client\CurlClient; |
||
| 14 | use OAuth\Common\Storage\Session; |
||
| 15 | use OAuth\OAuth2\Service\Nest; |
||
| 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['nest']['key'], |
||
| 28 | $servicesCredentials['nest']['secret'], |
||
| 29 | $currentUri->getAbsoluteUri() |
||
| 30 | ); |
||
| 31 | |||
| 32 | $serviceFactory->setHttpClient(new CurlClient()); |
||
| 33 | // Instantiate the Nest service using the credentials, http client and storage mechanism for the token |
||
| 34 | /** @var Nest $nestService */ |
||
| 35 | $nestService = $serviceFactory->createService('nest', $credentials, $storage); |
||
| 36 | |||
| 37 | if (!empty($_GET['code'])) { |
||
| 38 | // retrieve the CSRF state parameter |
||
| 39 | $state = $_GET['state'] ?? null; |
||
| 40 | // This was a callback request from nest, get the token |
||
| 41 | $token = $nestService->requestAccessToken($_GET['code'], $state); |
||
| 42 | // Show some of the resultant data |
||
| 43 | $result = json_decode($nestService->request('/devices'), true); |
||
| 44 | echo 'Your devices informations ' . print_r($result, true); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 45 | } elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { |
||
| 46 | $url = $nestService->getAuthorizationUri(); |
||
| 47 | header('Location: ' . $url); |
||
| 48 | } else { |
||
| 49 | $url = $currentUri->getRelativeUri() . '?go=go'; |
||
| 50 | echo "<a href='$url'>Login with Nest!</a>"; |
||
| 51 | } |
||
| 52 |