Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function admin_oauthCallBack($website_id = '') {
- $this->autoRender = false;
- $this->loadModel('Website');
- // Code begins
- $authorizationCode = '';
- $error = '';
- $oauthInfo = array();
- if (!empty($_REQUEST['code'])) {
- $authorizationCode = trim($_REQUEST['code']);
- }
- if (!empty($_REQUEST['error'])) {
- $error = trim($_REQUEST['error']);
- }
- if (!empty($_REQUEST['state'])) {
- $website_id = $_REQUEST['state'];
- }
- if (!empty($authorizationCode)) {
- $data = $this->getAccessToken($authorizationCode);
- //pr($data); die;
- if (!empty($data) && empty($data['error'])) {
- $accessToken = $data['access_token'];
- $refreshToken = $data['refresh_token'];
- // Customer Service (Single Adwords Account)
- $clientInfo = $this->getClientInfo($accessToken, $refreshToken);
- // Managed Customer Service (Manage Multiple Adwords Account)
- if (is_string($clientInfo)) {
- $this->redirect(array('controller' => 'websites', 'action' => 'listClients', $clientInfo, base64_encode($accessToken), base64_encode($refreshToken), base64_encode($website_id)));
- }
- if (!empty($clientInfo) && is_array($clientInfo)) {
- $oauth_register_email = $this->getOauthRegisterEmail($accessToken);
- $update_data = array(
- 'aw_oauth_refresh_token' => "'" . $clientInfo['oauth_refresh_token'] . "'",
- 'aw_oauth_register_email' => "'" . $oauth_register_email . "'",
- 'aw_client_id' => $clientInfo['client_id']
- );
- $condition = array('id' => $website_id);
- $this->Website->updateAll($update_data, $condition);
- $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true)); // clients
- } else {
- $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true));
- }
- } else {
- $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true));
- }
- } else if (!empty($error)) {
- $this->Session->setFlash('Access Denied by User.', 'default', array('class' => 'error'));
- $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true));
- } else {
- header("Location:https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=" . Configure::read('AW_CLIENT_ID') . "&redirect_uri=" . Configure::read('REDIRECT_URI') . "&access_type=offline&approval_prompt=force&state=" . $website_id . "&scope=https://adwords.google.com/api/adwords/email");
- die;
- }
- }
- function admin_linkAccount($client_id, $website_id, $access_token, $refresh_token) {
- $clientId = base64_decode($client_id);
- $websiteId = base64_decode($website_id);
- $accessToken = base64_decode($access_token);
- $refreshToken = base64_decode($refresh_token);
- $this->loadModel('Website');
- $oauth_register_email = $this->getOauthRegisterEmail($accessToken);
- $update_data = array(
- 'aw_oauth_refresh_token' => "'" . $refreshToken . "'",
- 'aw_oauth_register_email' => "'" . $oauth_register_email . "'",
- 'aw_client_id' => $clientId
- );
- $condition = array('id' => $websiteId);
- $this->Website->updateAll($update_data, $condition);
- $this->redirect(array('controller' => 'websites', 'action' => 'index', 'admin' => true));
- }
- function getClientInfo($access_token, $refresh_token) {
- $clients = array();
- try {
- $oauth2_info = array('client_id' => Configure::read('AW_CLIENT_ID'),
- 'client_secret' => Configure::read('AW_CLIENT_SECRET'),
- 'refresh_token' => $refresh_token
- );
- $user = new AdWordsUser();
- $user->SetOAuth2Info($oauth2_info);
- $user->LogAll();
- $customerService = $user->GetService('CustomerService', ADWORDS_VERSION);
- // Make the get request.
- $result = $customerService->get();
- if (!empty($result)) {
- $i = 0;
- $isMCC = $result->canManageClients;
- if (!$isMCC) {
- $clients['client_id'] = $result->customerId;
- $clients['oauth_refresh_token'] = $refresh_token;
- } else {
- return $result->customerId;
- }
- }
- } catch (Exception $e) {
- }
- return $clients;
- }
- function getOauthRegisterEmail($access_token) {
- $email = '';
- if (!empty($access_token)) {
- $url = "https://www.googleapis.com/oauth2/v2/userinfo?access_token=" . $access_token;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $Rec_Data = curl_exec($ch);
- if (curl_exec($ch) === false) {
- return $email;
- }
- $Rec_Data = json_decode($Rec_Data, true);
- if (!empty($Rec_Data['email'])) {
- $email = trim($Rec_Data['email']);
- }
- }
- return $email;
- }
- function getAccessToken($code) {
- $Rec_Data = array();
- if (!empty($code)) {
- $postFields = 'client_id=' . Configure::read('AW_CLIENT_ID') . '&client_secret=' . Configure::read('AW_CLIENT_SECRET') . '&code=' . $code . '&grant_type=authorization_code&redirect_uri=' . Configure::read('REDIRECT_URI');
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $Rec_Data = curl_exec($ch);
- if (curl_exec($ch) === false) {
- return $Rec_Data;
- //echo 'Curl error: ' . curl_error($ch);
- }
- $Rec_Data = json_decode($Rec_Data, true);
- }
- return $Rec_Data;
- }
- }
Add Comment
Please, Sign In to add comment