Skip to content

Commit f85f38a

Browse files
committed
Added google_offline example
1 parent 2e66b1d commit f85f38a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

examples/google_offline.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* Example of retrieving an authentication token of the Google service
5+
*
6+
* PHP version 5.4
7+
*
8+
* @author And <and.webdev@gmail.com>
9+
* @author David Desberg <david@daviddesberg.com>
10+
* @author Pieter Hordijk <info@pieterhordijk.com>
11+
* @copyright Copyright (c) 2015 The authors
12+
* @license http://www.opensource.org/licenses/mit-license.html MIT License
13+
*/
14+
15+
use OAuth\OAuth2\Service\Google;
16+
use OAuth\Common\Storage\Session;
17+
use OAuth\Common\Consumer\Credentials;
18+
19+
/**
20+
* Bootstrap the example
21+
*/
22+
require_once __DIR__ . '/bootstrap.php';
23+
24+
// Session storage
25+
$storage = new Session();
26+
27+
// Setup the credentials for the requests
28+
$credentials = new Credentials(
29+
$servicesCredentials['google']['key'],
30+
$servicesCredentials['google']['secret'],
31+
$currentUri
32+
);
33+
34+
// Instantiate the Google service using the credentials, http client and storage mechanism for the token
35+
/** @var Google $googleService */
36+
$googleService = $serviceFactory->createService('google', $credentials, $storage,
37+
[Google::SCOPE_USERINFO_EMAIL, Google::SCOPE_USERINFO_PROFILE]);
38+
39+
if (!empty($_GET['clear']) && $_GET['clear'] === 'clear') {
40+
$googleService->getStorage()->clearToken($googleService->service());
41+
header("Location: $currentUri", TRUE);
42+
} elseif ($googleService->isGlobalRequestArgumentsPassed() OR
43+
$googleService->getStorage()->hasAccessToken($googleService->service())) {
44+
45+
if (!$googleService->getStorage()->hasAccessToken($googleService->service())) $googleService->retrieveAccessTokenByGlobReqArgs();
46+
47+
// Retrieve a token and send a request
48+
$result = $googleService->requestJSON('https://www.googleapis.com/oauth2/v1/userinfo');
49+
50+
// Show some of the resultant data
51+
echo 'Your unique google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
52+
53+
echo '<br />';
54+
$extractor = $googleService->constructExtractor();
55+
echo 'Your extracted email is a: ' . $extractor->getEmail() . ', ' .
56+
'<br>and image: ' . inline_image($extractor->getImageRawData(50));
57+
58+
echo '<br />';
59+
echo '<br />';
60+
echo "<a href='$currentUri?clear=clear'>Clear offline access token</a>";
61+
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
62+
$googleService->setAccessType('offline')->redirectToAuthorizationUri();
63+
} else {
64+
echo "<a href='$currentUri?go=go'>Login with Google!</a>";
65+
}

src/OAuth2/Service/Google.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public function setAccessType($accessType)
113113
throw new InvalidAccessTypeException('Invalid accessType, expected either online or offline');
114114
}
115115
$this->accessType = $accessType;
116+
117+
return $this;
116118
}
117119

118120
/**

0 commit comments

Comments
 (0)