if (!isset($_SESSION['requestToken'])) { ?> <a href="index.php?action=requestToken">Click here</a> to <?php } ?> obtain temporary credentials </li> <!-- Step 2: authorize the temporary credentials --> <li><b>Step 2</b>: <?php if (isset($_SESSION['requestToken']) && !isset($_SESSION['oauthVerifier'])) { ?> <a href="<?php echo htmlspecialchars(getAuthorizationUrl()); ?> ">Click here</a> to <?php } ?> authorize the temporary credentials </li> <!-- Step 3: exchange the authorized temporary credentials for token credentials --> <li><b>Step 3</b>: <?php if (isset($_SESSION['requestToken']) && isset($_SESSION['oauthVerifier']) && !isset($_SESSION['accessToken'])) { ?> <a href="index.php?action=accessToken">Click here</a> to <?php
<?php require_once "functions.php"; session_start(); header('Content-Type: text/html; charset=utf-8'); $authUrl = getAuthorizationUrl("", ""); ?> <!DOCTYPE html> <html lang="fi"> <head> <title>Google Drive Login and Upload</title> <meta charset="UTF-8"> </head> <body> <a href=<?php echo "'" . $authUrl . "'"; ?> >Authorize</a> </body> </html>
/** * Retrieve credentials using the provided authorization code. * * This function exchanges the authorization code for an access token and * queries the UserInfo API to retrieve the user's e-mail address. If a * refresh token has been retrieved along with an access token, it is stored * in the application database using the user's e-mail address as key. If no * refresh token has been retrieved, the function checks in the application * database for one and returns it if found or throws a NoRefreshTokenException * with the authorization URL to redirect the user to. * * @param String authorizationCode Authorization code to use to retrieve an access * token. * @param String state State to set to the authorization URL in case of error. * @return String Json representation of the OAuth 2.0 credentials. * @throws NoRefreshTokenException No refresh token could be retrieved from * the available sources. */ function getCredentials($authorizationCode, $state) { $emailAddress = ''; try { $credentials = $this->exchangeCode($authorizationCode); $userInfo = $this->getUserInfo($credentials); $emailAddress = $userInfo->getEmail(); $userId = $userInfo->getId(); $credentialsArray = json_decode($credentials, true); if (isset($credentialsArray['refresh_token'])) { $this->storeCredentials($userId, $credentials); return $credentials; } else { $credentials = $this->getStoredCredentials($userId); $credentialsArray = json_decode($credentials, true); if ($credentials != null && isset($credentialsArray['refresh_token'])) { return $credentials; } } } catch (CodeExchangeException $e) { print 'An error occurred during code exchange.'; // Drive apps should try to retrieve the user and credentials for the current // session. // If none is available, redirect the user to the authorization URL. $e->setAuthorizationUrl(getAuthorizationUrl($emailAddress, $state)); throw $e; } catch (NoUserIdException $e) { print 'No e-mail address could be retrieved.'; } // No refresh token has been retrieved. $authorizationUrl = getAuthorizationUrl($emailAddress, $state); throw new NoRefreshTokenException($authorizationUrl); }
// Status variables $lastError = null; $currentStatus = null; // Request dispatching. If a function fails, $lastError will be updated. if (isset($_GET['action'])) { $action = $_GET['action']; if ($action == 'callback') { if (handleCallback()) { if (getTokenCredentials()) { listNotebooks(); } } } elseif ($action == 'authorize') { if (getTemporaryCredentials()) { // We obtained temporary credentials, now redirect the user to evernote.com to authorize access header('Location: ' . getAuthorizationUrl()); } } elseif ($action == 'reset') { resetSession(); } } ?> <html> <head> <title>Evernote PHP OAuth Demo</title> </head> <body> <h1>Evernote PHP OAuth Demo</h1>