/** * reads G+ basic user profile data as "Dispaly Name", "id", "Gender" if uathorized "emails" * * @param * * $params <b>array</b> * array(<br> * 'auth_token'=>access token, // (optional)<br> * 'auth_refresh_token'=> refresh token,// (optional)<br> * 'user_id'=> system user id, // (required)<br> * )<br> * * @return void To access prifile propert use object getters: * <p> * 1. G+ account emails - $this->GetUserEmail() * 2. G+ user display name - $this->GetName() * 3. G+ user id - $this->GetMediaUserId() * </p> */ public function MeAction($params) { $ACCESS_TOKEN = isset($params[self::AUTH_TOKEN]) ? $params[self::AUTH_TOKEN] : null; $REFRESH_TOKEN = isset($params[self::AUTH_REFRESH_TOKEN]) ? $params[self::AUTH_REFRESH_TOKEN] : null; $USER_ID = isset($params[self::USER_ID]) ? $params[self::USER_ID] : null; // 1. If IS NOT set access token and refresh token - get fro DB by USER_ID if (empty($ACCESS_TOKEN) && empty($REFRESH_TOKEN)) { if (empty($USER_ID)) { throw new \Google_Exception('To get access token and refresh token you need to supply USER_ID'); } $params_social = array(\Av\MediaUserModel::MEDIA_ID => \Av\MediaModel::MEDIA_GOOGLE_PLUS, \Av\MediaUserModel::USER_ID => $USER_ID); $oSocialUserMapper = new \Av\MediaUserModel(); $access_token_info = $oSocialUserMapper->GetCredentials($params_social); if ($access_token_info === false) { throw new \Google_Exception("No access token and refresh token are saved for USER_ID {$USER_ID}"); } $ACCESS_TOKEN = isset($access_token_info[\Av\MediaUserModel::ACCESS_TOKEN]) ? $access_token_info[\Av\MediaUserModel::ACCESS_TOKEN] : null; $REFRESH_TOKEN = isset($access_token_info[\Av\MediaUserModel::REFRESH_TOKEN]) ? $access_token_info[\Av\MediaUserModel::REFRESH_TOKEN] : null; if (empty($ACCESS_TOKEN) || empty($REFRESH_TOKEN)) { throw new \Google_Exception("access token OR refresh token is missing for USER_ID {$USER_ID}"); } } // 2. Get app credentials $oMediaClients = new \Av\ClientCredentialsModel(); $params_credentials = array(\Av\ClientCredentialsModel::MEDIA_ID => \Av\MediaModel::MEDIA_GOOGLE_PLUS); $credential = $oMediaClients->GetCredentials($params_credentials); $CLIENT_ID = isset($credential[\Av\ClientCredentialsModel::CLIENT_ID]) ? $credential[\Av\ClientCredentialsModel::CLIENT_ID] : null; $CLIENT_SECRET = isset($credential[\Av\ClientCredentialsModel::CLIENT_SECRET]) ? $credential[\Av\ClientCredentialsModel::CLIENT_SECRET] : null; if (!isset($CLIENT_ID) || !isset($CLIENT_SECRET)) { throw new \Google_Exception('You must fill out the CLIENT credentials'); } // $this->AddDebug(__METHOD__ . " #" .__LINE__ . " acess " . print_r($ACCESS_TOKEN,true)); // $this->AddDebug(__METHOD__ . " #" .__LINE__ . " refresh " . print_r($REFRESH_TOKEN,true)); // // return; // 3. Check if access token is valid, if not, get new by refresh token $oTokenValidator = new GoogleAuthResponseController(); $params_valid_access_token = array(self::AUTH_TOKEN => $ACCESS_TOKEN, self::AUTH_REFRESH_TOKEN => $REFRESH_TOKEN, self::USER_ID => $USER_ID, self::MEDIA_ID => \Av\MediaModel::MEDIA_GOOGLE_PLUS, self::CLIENT_ID => $CLIENT_ID, self::CLIENT_SECRET => $CLIENT_SECRET); $ACCESS_TOKEN = $oTokenValidator->getValidAccessToken($params_valid_access_token); // 4. Create plus_service $client = new \Google_Client(); $client->setClientId($CLIENT_ID); $client->setClientSecret($CLIENT_SECRET); $client->setAccessToken($ACCESS_TOKEN); //grant_type=refresh_token $oMeDataService = new \Google_Service_Plus($client); // 4. Get Me data $oMe = $oMeDataService->people->get('me'); // $this->AddDebug(__METHOD__ . " #" . __LINE__ . " me:" . print_r($oMe, true)); $this->SetName($oMe->getDisplayName()); // $this->AddDebug(__METHOD__ . " #" . __LINE__ . " my emails are:" . print_r($arrEmails, true)); $this->SetUserEmail($oMe->getEmails()); $this->SetMediaUserId($oMe->getId()); $this->SetVerified($oMe->getVerified()); $this->SetUserImage($oMe->getImage()); $this->SetUserAboutMe($oMe->getAboutMe()); }
public function GetAuthResponse($params) { if (isset($params[self::CODE]) && $params[self::CODE] != 400) { // echo __METHOD__ . __LINE__ . " user id {$usr_id} / CODE {$params['code']} user brands <br>"; $STATE = isset($params[self::STATE]) ? $params[self::STATE] : null; /* * Get Application credentials: client id / client secret */ $oMediaClients = new \Av\ClientCredentialsModel(); $params_credentials = array(\Av\ClientCredentialsModel::MEDIA_ID => \Av\MediaModel::MEDIA_FACEBOOK); $credential = $oMediaClients->GetCredentials($params_credentials); $APP_ID = isset($credential[\Av\ClientCredentialsModel::CLIENT_ID]) ? $credential[\Av\ClientCredentialsModel::CLIENT_ID] : null; $APP_SECRET = isset($credential[\Av\ClientCredentialsModel::CLIENT_SECRET]) ? $credential[\Av\ClientCredentialsModel::CLIENT_SECRET] : null; if (!isset($APP_ID) || !isset($APP_SECRET)) { throw new FacebookSDKException('You must fill out the CLIENT credentials'); } FacebookSession::setDefaultApplication($APP_ID, $APP_SECRET); require_once 'Facebook/FacebookRedirectLoginHelper.php'; /* * Get callback end point */ $oCallBackUrl = new \Av\MediaAuthCallbackUrlModel(); $params_callback_url = array(\Av\MediaAuthCallbackUrlModel::MEDIA_ID => \Av\MediaModel::MEDIA_FACEBOOK, \Av\MediaAuthCallbackUrlModel::AUTH_TYPE_ID => \Av\MediaAuthTypeModel::AUTH_TYPE_SIGNUP); $callback_url = $oCallBackUrl->GetMediaAuthCallbackUrl($params_callback_url); if ($callback_url === false) { throw new FacebookSDKException("Callback Url for media FACEBOOK and Authorization type Signup is not set"); } $helper = new FacebookRedirectLoginHelper($callback_url); try { $session = $helper->getSessionFromRedirect(); $ACCESS_TOKEN = $session->getToken(); $this->service_auth->SetToken($ACCESS_TOKEN); if (isset($params[self::STATE])) { $arrState = \Av\UrlControllerHelper::unpackParamFromUrl($params[self::STATE]); $this->service_auth->SetState($arrState); } } catch (FacebookRequestException $ex) { echo __METHOD__ . __LINE__ . " Facebook Authentication error <br><pre>"; var_dump($ex); echo "</pre>"; return; } catch (\Exception $ex) { echo __METHOD__ . __LINE__ . " Facebook error during authentication <br><pre>"; var_dump($ex); echo "</pre>"; return; } $params_access_token = array(self::AUTH_TOKEN => $ACCESS_TOKEN); try { $this->service_person->MeAction($params_access_token); } catch (FacebookRequestException $e) { echo __METHOD__ . __LINE__ . " Facebook error during getting person info <br><pre>"; var_dump($e); echo "</pre>"; return; } // get granted permissions try { $this->service_person->PermissionsAction($params_access_token); } catch (FacebookRequestException $e) { echo __METHOD__ . __LINE__ . " Facebook error during getting permissions <br><pre>"; var_dump($e); echo "</pre>"; return; } } else { echo __METHOD__ . __LINE__ . " Facebook CODE is not set or set to 400<br>"; } }
public function getValidAccessToken($params) { $CLIENT_ID = isset($params[self::CLIENT_ID]) ? $params[self::CLIENT_ID] : null; $CLIENT_SECRET = isset($params[self::CLIENT_SECRET]) ? $params[self::CLIENT_SECRET] : null; $USER_ID = isset($params[self::USER_ID]) ? $params[self::USER_ID] : null; $MEDIA_ID = isset($params[self::MEDIA_ID]) ? $params[self::MEDIA_ID] : null; $ACCESS_TOKEN = isset($params[self::AUTH_TOKEN]) ? $params[self::AUTH_TOKEN] : null; $REFRESH_TOKEN = isset($params[self::AUTH_REFRESH_TOKEN]) ? $params[self::AUTH_REFRESH_TOKEN] : null; if (!isset($CLIENT_ID) || !isset($CLIENT_SECRET)) { $oMediaClients = new \Av\ClientCredentialsModel(); $params_credentials = array(\Av\ClientCredentialsModel::MEDIA_ID => $MEDIA_ID); $credential = $oMediaClients->GetCredentials($params_credentials); if ($credential === false) { throw new \Google_Exception('You must fill out the CLIENT credentials'); } $CLIENT_ID = isset($credential[\Av\ClientCredentialsModel::CLIENT_ID]) ? $credential[\Av\ClientCredentialsModel::CLIENT_ID] : null; $CLIENT_SECRET = isset($credential[\Av\ClientCredentialsModel::CLIENT_SECRET]) ? $credential[\Av\ClientCredentialsModel::CLIENT_SECRET] : null; } // 2. Find authorization credentials for given user/media $oMediaUserMapper = new \Av\MediaUserModel(); if (empty($ACCESS_TOKEN)) { // 3. if access token is not set - this call is made by server, without user contenst if (empty($USER_ID) || empty($MEDIA_ID)) { throw new \Google_Exception('You must passt the USER ID and MEDIA ID parameter to get access and refresh tokens from DB'); } $params_user_credentials = array(\Av\MediaUserModel::MEDIA_ID => $MEDIA_ID, \Av\MediaUserModel::USER_ID => $USER_ID); $arrUserCredentials = $oMediaUserMapper->GetCredentials($params_user_credentials); if (empty($arrUserCredentials[\Av\MediaUserModel::REFRESH_TOKEN])) { throw new \Google_Exception("Refresh token is not set for user id {$USER_ID} "); } if (empty($arrUserCredentials[\Av\MediaUserModel::ACCESS_TOKEN])) { throw new \Google_Exception("Access token is not set for user id {$USER_ID} "); } // 3. Extract access token $ACCESS_TOKEN = $arrUserCredentials[\Av\MediaUserModel::ACCESS_TOKEN]; $REFRESH_TOKEN = $arrUserCredentials[\Av\MediaUserModel::REFRESH_TOKEN]; } // 4. check if the access token valid $client = new \Google_Client(); $client->setClientId($CLIENT_ID); $client->setClientSecret($CLIENT_SECRET); $client->setAccessToken($ACCESS_TOKEN); if ($client->isAccessTokenExpired()) { // 4.1 Old access token is not valid - get new access token if (isset($REFRESH_TOKEN)) { $client->refreshToken($REFRESH_TOKEN); } else { throw new \Google_Exception("Invalid refresh token."); } // 4.2 save new access token in DB $ACCESS_TOKEN = $client->getAccessToken(); if (isset($ACCESS_TOKEN)) { if (empty($USER_ID) || empty($MEDIA_ID)) { throw new \Google_Exception('You must passt the USER ID and MEDIA ID parameter to get access and refresh tokens from DB'); } $params_new_credentials = array(\Av\MediaUserModel::ACCESS_TOKEN => $ACCESS_TOKEN, \Av\MediaUserModel::MEDIA_ID => $MEDIA_ID, \Av\MediaUserModel::USER_ID => $USER_ID); $oMediaUserMapper->SaveCredentials($params_new_credentials); } else { throw new \Google_Exception("Could not refresh access token."); } } return $ACCESS_TOKEN; }
public function GetLink($params = null) { /* * Good chance to read some extra parameners from $params */ if (isset($params)) { } /* * Get main parameters set in constractor */ $MEDIA_ID = $this->GetMediaID(); $AUTH_TYPE_ID = $this->GetAuthTypeID(); $STATE = $this->GetState(); if (empty($MEDIA_ID) || empty($AUTH_TYPE_ID)) { return false; } /* * Get Application credentials: client id / client secret */ $oMediaClients = new \Av\ClientCredentialsModel(); $params_credentials = array(\Av\ClientCredentialsModel::MEDIA_ID => $MEDIA_ID); $credential = $oMediaClients->GetCredentials($params_credentials); if (isset($credential[\Av\ClientCredentialsModel::CLIENT_ID])) { $this->SetClientId($credential[\Av\ClientCredentialsModel::CLIENT_ID]); } if (isset($credential[\Av\ClientCredentialsModel::CLIENT_SECRET])) { $this->SetClientSecret($credential[\Av\ClientCredentialsModel::CLIENT_SECRET]); } /* * Get authorization scope */ $oScope = new \Av\MediaAuthScopeModel(); $params_scope = array(\Av\MediaAuthScopeModel::MEDIA_ID => $MEDIA_ID, \Av\MediaAuthScopeModel::AUTH_TYPE_ID => $AUTH_TYPE_ID); $scope = $oScope->GetMediaAuthScope($params_scope); if ($scope !== false) { $this->SetScope($scope); } /* * Get callback end point */ $oCallBackUrl = new \Av\MediaAuthCallbackUrlModel(); $params_callback_url = array(\Av\MediaAuthCallbackUrlModel::MEDIA_ID => $MEDIA_ID, \Av\MediaAuthCallbackUrlModel::AUTH_TYPE_ID => $AUTH_TYPE_ID); $callback_url = $oCallBackUrl->GetMediaAuthCallbackUrl($params_callback_url); if ($callback_url !== false) { $this->SetCallbackUrl($callback_url); } return $this->BuildLink($params); }