/** * Gets an access token that will be cached using a file. * * This method works first trying to load the file specified in config, * and, if a saved OAuth token isn't found, this method will send an API * request. The OAuth token will then be saved 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 file IO issue */ function getFileToken() { $token = OAuthToken::loadToken(OAUTH_FILE); if ($token == null || $token->isAccessTokenExpired()) { $tokenSrvc = new OAuthTokenService(FQDN, CLIENT_ID, CLIENT_SECRET); $token = $tokenSrvc->getTokenUsingScope(SCOPE); // save token for future use $token->saveToken(OAUTH_FILE); } return $token; }
/** * Gets an access token that will be cached using a file. * * This method works first trying to load the file specified in config, * and, if a saved OAuth token isn't found, this method will send an API * request. The OAuth token will then be saved 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 file IO issue */ function getFileToken() { require __DIR__ . '/../config.php'; // maintain backwards compability with older configs if (isset($api_key)) { $clientId = $api_key; } if (isset($secret_key)) { $clientSecret = $secret_key; } if (isset($oauth_file)) { $oauthFile = $oauth_file; } $token = OAuthToken::loadToken($oauthFile); if ($token == null || $token->isAccessTokenExpired()) { $tokenSrvc = new OAuthTokenService($FQDN, $clientId, $clientSecret); $token = $tokenSrvc->getTokenUsingScope($scope); // save token for future use $token->saveToken($oauthFile); } return $token; }
* See the License for the specific language governing permissions and * limitations under the License. */ session_start(); require __DIR__ . '/../config.php'; require_once __DIR__ . '/../lib/OAuth/OAuthToken.php'; require_once __DIR__ . '/../lib/OAuth/OAuthTokenService.php'; require_once __DIR__ . '/../lib/Util/Util.php'; require_once __DIR__ . '/../lib/Speech/SpeechService.php'; use Att\Api\OAuth\OAuthToken; use Att\Api\OAuth\OAuthTokenService; use Att\Api\Util\Util; use Att\Api\Speech\SpeechService; $arr = null; try { $token = OAuthToken::loadToken($oauth_file); if ($token == null || $token->isAccessTokenExpired()) { $tokenSrvc = new OAuthTokenService($FQDN, $api_key, $secret_key); $token = $tokenSrvc->getTokenUsingScope($scope); $token->saveToken($oauth_file); } $speechSrvc = new SpeechService($FQDN, $token); $speechContext = $_POST['speechContext']; $speechFile = $_POST['speechFile']; $xArg = $_POST['x_arg']; $subContext = $_POST['x_subcontext']; $chunked = isset($_POST['chunked']) ? true : null; $allowedFiles = array('boston_celtics.wav', 'california.amr', 'coffee.amr', 'doctors.wav', 'nospeech.wav', 'samplerate_conflict_error.wav', 'this_is_a_test.spx', 'too_many_channels_error.wav', 'boston_celtics.wav'); if (!in_array($speechFile, $allowedFiles, true)) { throw new Exception('Invalid speech file specified'); }
/** * Gets a new OAuth token using the refresh token of the specified OAuth * token. * * The token request is done using the <i>refresh_token</i> grant type. * * @param OAuthToken $token OAuth token to use for refreshing * * @return OAuthToken an OAuth token * @throws OAuthException if server did not return valid access token */ public function refreshToken(OAuthToken $token) { $httpPost = new HttpPost(); $httpPost->setParam('refresh_token', $token->getRefreshToken())->setParam('grant_type', 'refresh_token')->setParam('client_id', $this->_clientId)->setParam('client_secret', $this->_clientSecret); $req = new RestfulRequest($this->_url); $result = $req->sendHttpPost($httpPost); return $this->parseResult($result); }
/** * Convenience method used to set the 'Authorization' header. * * @param OAuthToken $token token to use when setting authorization header. * * @return a reference to this, thereby allowing method chaining */ public function setAuthorizationHeader(OAuthToken $token) { $this->setHeader('Authorization', 'BEARER ' . $token->getAccesstoken()); return $this; }
/** * Gets an access token that will be cached using a file. * * This method works first trying to load the file specified in config, * and, if a saved OAuth token isn't found, this method will send an API * request. The OAuth token will then be saved 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 file IO issue */ protected function getFileToken() { // load location where to save token include __DIR__ . '/../../config.php'; if (!isset($oauth_file)) { // set default if can't load $oauth_file = 'token.php'; } $token = OAuthToken::loadToken($oauth_file); if ($token == null || $token->isAccessTokenExpired()) { $tokenSrvc = new OAuthTokenService($this->oauthFQDN, $this->clientId, $this->clientSecret); $token = $tokenSrvc->getTokenUsingScope($this->scope); // save token for future use $token->saveToken($oauth_file); } return $token; }