/** * constructor * * @param ConfigRepository $config */ public function __construct(ConfigRepository $config) { $this->config = $config; $this->bazaarApi = new \Nikapps\BazaarApiPhp\BazaarApi(); //add configurations $this->bazaarApi->setAccountConfig($this->getAccountConfig()); $this->bazaarApi->setApiConfig($this->getApiConfig()); //create token manager $tokenManager = new CacheTokenManager(); $tokenManager->setCacheDriver($this->config->get('bazaar-api-laravel::cache.cache_driver')); $tokenManager->setCacheName($this->config->get('bazaar-api-laravel::cache.cache_name')); $this->bazaarApi->setTokenManager($tokenManager); }
/** * Execute the console command. * * @return mixed */ public function fire() { $code = $this->option('code'); $redirectUri = $this->option('redirect-uri'); $apiConfig = $this->getApiConfig(); $accountConfig = $this->getAccountConfig($code, $redirectUri); $bazaarApi = new BazaarApi(); $bazaarApi->setApiConfig($apiConfig); $bazaarApi->setAccountConfig($accountConfig); $this->line(sprintf($this->generateCodeUri, $accountConfig->getRedirectUri(), $accountConfig->getClientId())); try { $fetchedRefreshToken = $bazaarApi->fetchRefreshToken(new AuthorizationRequest()); $json = $fetchedRefreshToken->getResponseJson(); $headers = array_keys($json); $rows = [$json]; $this->info("Refresh Token: " . $fetchedRefreshToken->getRefreshToken()); $this->comment('Save refresh token in your config file'); $this->table($headers, $rows); } catch (NetworkErrorException $e) { $this->error('Error: ' . $e->getClientException()->getRequest()->getUrl()); $this->error('Response Status: ' . $e->getClientException()->getResponse()->getStatusCode()); if ($e->getClientException()->getResponse()->getStatusCode() == 401) { $this->comment('Your credentials is invalid or you should generate new code from this url: '); $this->line(sprintf($this->generateCodeUri, $accountConfig->getRedirectUri(), $accountConfig->getClientId())); } } catch (\Exception $e) { $this->error("Unknown Error: " . $e->getMessage()); } }