public static function authenticate($api_key, $api_secret, $captcha, $captcha_token) { try { $api = new GoogleUtilityClient($api_key, $api_secret, 'HOSTED_OR_GOOGLE', $captcha, $captcha_token); $api->authenticate(); return TRUE; } catch (GoogleUtilityClientException $e) { switch (GoogleLoginChallenge::get_error($e->getCode())) { case 'CaptchaRequired': $captchaException = new GoogleCaptchaChallengeException($e->getMessage(), $e->getCode()); $captchaException->captcha_url = $api->auth_response['CaptchaUrl']; $captchaException->captcha_token = $api->auth_response['CaptchaToken']; throw $captchaException; } throw new GoogleDomainException($e->getMessage(), $e->getCode()); } }
public function authenticate() { $auth_url = "https://www.google.com/accounts/ClientLogin"; if (is_null(self::$curl)) { self::$curl = curl_init(); } $args = array('Email' => $this->api_key, 'Passwd' => $this->api_secret, 'accountType' => $this->account_type, 'service' => 'apps', 'source' => 'twilio-openVBX-1.0', 'logintoken' => $this->login_token, 'logincaptcha' => $this->login_captcha); error_log(var_export($args, true)); curl_setopt(self::$curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); curl_setopt(self::$curl, CURLOPT_POST, true); curl_setopt(self::$curl, CURLOPT_POSTFIELDS, http_build_query($args)); curl_setopt(self::$curl, CURLOPT_RETURNTRANSFER, true); curl_setopt(self::$curl, CURLOPT_URL, $auth_url); $response = curl_exec(self::$curl); $response = explode("\n", $response); $auth_response = array(); foreach ($response as $response_pair) { $response_pair = explode('=', $response_pair); $auth_response[$response_pair[0]] = isset($response_pair[1]) ? $response_pair[1] : ''; $auth_response[$response_pair[0]] .= isset($response_pair[2]) ? '=' . $response_pair[2] : ''; } if (!empty($auth_response['Auth'])) { $this->auth_token = $auth_response['Auth']; $this->authenticated = true; return $this->authenticated; } if (!empty($auth_response['Error'])) { $this->auth_response = $auth_response; $error_code = GoogleLoginChallenge::get_error_code($auth_response['Error']); $error_message = GoogleLoginChallenge::get_error_message($auth_response['Error']); throw new GoogleUtilityClientException($error_message, $error_code); } }