public function process() { $code = $this->getProperty('code'); if (!empty($code)) { $client = $this->bigbrother->loadOAuth(); // https://developers.google.com/identity/protocols/OAuth2InstalledApp $authParams = array('code' => $code, 'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob', 'scope' => 'https://www.googleapis.com/auth/analytics.readonly', 'grant_type' => 'authorization_code'); $result = false; try { $result = $client->getAccessToken($this->bigbrother->oauthTokenEndpoint, 'authorization_code', $authParams); } catch (Exception $e) { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Exception during getAccessToken: ' . $e->getMessage()); } if (is_array($result) && $result['code'] == 200) { $accessToken = $result['result']['access_token']; $refreshToken = $result['result']['refresh_token']; $expiresIn = $result['result']['expires_in']; $this->modx->getCacheManager()->set('access_token', $accessToken, $expiresIn, $this->bigbrother->cacheOptions); $this->bigbrother->updateOption('refresh_token', $refreshToken, 'text-password'); return $this->success('', array('text' => $this->modx->lexicon('bigbrother.authorize_success'))); } return $this->failure('Unable to complete oAuth2 flow: <pre>' . print_r($result, true) . '</pre>'); } return $this->failure('No code provided.'); }
public function process() { if (!$this->ga->loadOAuth()) { return $this->failure($this->modx->lexicon('bigbrother.err_load_oauth')); } $result = $this->callAPI($this->ga->baseUrl . 'management/accounts/~all/webproperties/~all/profiles'); if (!empty($this->error)) { return $this->failure($this->error); } $output = $account = array(); $assign = $this->getProperty('assign', false); if ($assign) { $account['name'] = $this->modx->lexicon('bigbrother.user_account_default'); $account['id'] = $this->modx->lexicon('bigbrother.user_account_default'); $output[] = $account; } $total = 0; // Get account list foreach ($result['items'] as $value) { $account['id'] = $value['id']; $account['name'] = $value['name']; $account['websiteUrl'] = $value['websiteUrl']; $account['webPropertyId'] = $value['webPropertyId']; $output[] = $account; $total += 1; } //$this->ga->updateOption('total_account', $result['totalResults']); $this->ga->updateOption('total_account', $total); return $this->success('', $output); }