PayPal\Auth\OAuthTokenCredential::getAccessToken PHP Method

getAccessToken() public method

Get AccessToken
public getAccessToken ( $config ) : null | string
$config
return null | string
    public function getAccessToken($config)
    {
        // Check if we already have accessToken in Cache
        if ($this->accessToken && time() - $this->tokenCreateTime < $this->tokenExpiresIn - self::$expiryBufferTime) {
            return $this->accessToken;
        }
        // Check for persisted data first
        $token = AuthorizationCache::pull($config, $this->clientId);
        if ($token) {
            // We found it
            // This code block is for backward compatibility only.
            if (array_key_exists('accessToken', $token)) {
                $this->accessToken = $token['accessToken'];
            }
            $this->tokenCreateTime = $token['tokenCreateTime'];
            $this->tokenExpiresIn = $token['tokenExpiresIn'];
            // Case where we have an old unencrypted cache file
            if (!array_key_exists('accessTokenEncrypted', $token)) {
                AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn);
            } else {
                $this->accessToken = $this->decrypt($token['accessTokenEncrypted']);
            }
        }
        // Check if Access Token is not null and has not expired.
        // The API returns expiry time as a relative time unit
        // We use a buffer time when checking for token expiry to account
        // for API call delays and any delay between the time the token is
        // retrieved and subsequently used
        if ($this->accessToken != null && time() - $this->tokenCreateTime > $this->tokenExpiresIn - self::$expiryBufferTime) {
            $this->accessToken = null;
        }
        // If accessToken is Null, obtain a new token
        if ($this->accessToken == null) {
            // Get a new one by making calls to API
            $this->updateAccessToken($config);
            AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn);
        }
        return $this->accessToken;
    }

Usage Example

Beispiel #1
0
 public function authorize()
 {
     $credentials = new OAuthTokenCredential($this->clientId, $this->clientSecret);
     if (is_null($this->_token)) {
         $credentials->getAccessToken(['mode' => 'sandbox']);
     }
     $this->_apiContext = new ApiContext($credentials);
 }
All Usage Examples Of PayPal\Auth\OAuthTokenCredential::getAccessToken