/** * Gets an access token that will be cached using a session. * * This method works by first trying to load the token from the user's * session, and, if a saved OAuth token isn't found, this method will send * an API request. The OAuth token will then be saved in session for future * use. * * @return OAuthToken OAuth token that can be used for authorization * @throws OAuthException if API request was not successful or if * there was a session issue */ protected function getSessionToken() { // Try loading token from session $token = isset($_SESSION['token']) ? unserialize($_SESSION['token']) : null; // load redirect URL include __DIR__ . '/../../config.php'; // No token or token is expired... send token request if (!$token || $token->isAccessTokenExpired()) { $codeURL = $this->oauthFQDN . '/oauth/authorize'; $codeRequest = new OAuthCodeRequest($codeURL, $this->clientId, $this->scope, $authorize_redirect_uri); $code = $codeRequest->getCode(); $tokenSrvc = new OAuthTokenService($this->oauthFQDN, $this->clientId, $this->clientSecret); $token = $tokenSrvc->getTokenUsingCode($code); $_SESSION['token'] = serialize($token); } return $token; }
/** * Retrieves an access token from AT&T once the user has authorized the application and has returned with an auth code * * @method getToken * @param {string} code The code returned by the authorize operation. * * @return {Response} Returns Response object */ public function getToken($code) { $oauthcode = new OAuthCode($code); // Create service for requesting an OAuth token $osrvc = new OAuthTokenService($this->base_url, $this->client_id, $this->client_secret); // Get OAuth token return $osrvc->getTokenUsingCode($oauthcode); }
// Enter the value from the 'App Key' field obtained at developer.att.com // in your app account. $clientId = 'ENTER VALUE!'; // Enter the value from the 'App Secret' field obtained at developer.att.com // in your app account. $clientSecret = 'ENTER VALUE!'; // Get the OAuth code by opening a browser to the following URL: // https://api.att.com/oauth/v4/authorize?client_id=CLIENT_ID&scope=SCOPE&redirect_uri=REDIRECT_URI // replacing CLIENT_ID, SCOPE, and REDIRECT_URI with the values configured at // developer.att.com. // After authenticating, copy the OAuth code from the browser URL. $oauthCode = "ENTER VALUE!"; // Create the service for requesting an OAuth access token $osrvc = new OAuthTokenService('https://api.att.com', $clientId, $clientSecret); // Get the OAuth token using the OAuth code. $token = $osrvc->getTokenUsingCode(new OAuthCode($oauthCode)); // Create the service for interacting with the In-App Messaging API. $immnSrvc = new IMMNService('https://api.att.com', $token); // The following lines of code can be used to test the method calls for // the IMMNService class. To test a specific method, comment out // the other method. /* This try/catch block tests the sendMessage method. */ try { // Specify the address to where the message is sent. $addr = array('ENTER VALUE!'); // Send a test message. $response = $immnSrvc->sendMessage($addr, 'Text', 'Subject'); echo "msgId: " . $response . "\n"; } catch (ServiceException $se) { echo $se->getErrorResponse(); }
/** * Gets an access token that will be cached using the user's session. * * This method works by first trying to load the token from the user's * session, and, if a saved OAuth token isn't found, this method will send * an API request. * * @return OAuthToken OAuth token that can be used for authorization * @throws OAuthException if API request was not successful or if * there was a session issue */ function getSessionToken() { // Try loading token from session $token = isset($_SESSION['token']) ? unserialize($_SESSION['token']) : null; // No token or token is expired... send token request if (!$token || $token->isAccessTokenExpired()) { // check for error/error description in request params if (isset($_REQUEST['error'])) { $error = $_REQUEST['error']; $errorDesc = ''; if (isset($_REQUEST['error_description'])) { $errorDesc = $_REQUEST['error_description']; } throw new OAuthException($error, $errorDesc); } $code = null; // check for code in request params if (isset($_REQUEST['code'])) { $code = new OAuthCode($_REQUEST['code']); } else { $error = 'Invalid state'; $errrorDesc = 'No code found in parameter'; throw new OAuthException($error, $errrorDesc); } $fqdn = getFqdn(); $tokenSrvc = new OAuthTokenService($fqdn, CLIENT_ID, CLIENT_SECRET); $token = $tokenSrvc->getTokenUsingCode($code); $_SESSION['token'] = serialize($token); } return $token; }
use Att\Api\OAuth\OAuthTokenService; use Att\Api\OAuth\OAuthCode; use Att\Api\Restful\RestfulEnvironment; use Exception; if (isset($proxy_host) && isset($proxy_port)) { RestfulEnvironment::setProxy($proxy_host, $proxy_port); } if (isset($accept_all_certs)) { RestfulEnvironment::setAcceptAllCerts($accept_all_certs); } try { $tokenSrvc = new OAuthTokenService($FQDN, $api_key, $secret_key); $token = null; if (isset($_POST['code'])) { $code = new OAuthCode($_POST['code']); $token = $tokenSrvc->getTokenUsingCode($code); } else { $token = $tokenSrvc->getTokenUsingScope('WEBRTC'); } $_SESSION['token'] = serialize($token); $arr = array('access_token' => $token->getAccessToken(), 'refresh_token' => $token->getRefreshToken(), 'expires_in' => $token->getExpiresIn()); echo json_encode($arr); } catch (Exception $e) { if (function_exists('http_response_code')) { http_response_code(500); } else { header("HTTP/1.1 500 Internal Server Error"); } echo json_encode($e->getMessage()); } /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */