You are here

protected function gapi::authenticateUser in Google Analytics Statistics 7

Same name and namespace in other branches
  1. 7.x inc/gapi.class.php \gapi::authenticateUser()

Authenticate Google Account with Google

Parameters

String $email:

String $password:

1 call to gapi::authenticateUser()
gapi::__construct in includes/gapi.class.php
Constructor function for all new gapi instances

File

includes/gapi.class.php, line 403

Class

gapi
GAPI - Google Analytics PHP Interface

Code

protected function authenticateUser($email, $password, $type = NULL) {

  // Updated by arpieb to allow explicit type to be optionally passed in
  if (!$type) {

    // Updated by jec006 to handle apps accounts also
    if (stristr($email, 'gmail.com') || stristr($email, 'mail.google.com')) {
      $type = 'GOOGLE';
    }
    else {
      $type = 'HOSTED';
    }
  }
  $post_variables = array(
    'accountType' => $type,
    'Email' => $email,
    'Passwd' => $password,
    'source' => gapi::interface_name,
    'service' => 'analytics',
  );

  // end jec006 mod
  $response = $this
    ->httpRequest(gapi::client_login_url, null, $post_variables);

  //Convert newline delimited variables into url format then import to array
  parse_str(str_replace(array(
    "\n",
    "\r\n",
  ), '&', $response['body']), $auth_token);
  if (substr($response['code'], 0, 1) != '2' || !is_array($auth_token) || empty($auth_token['Auth'])) {
    throw new Exception('GAPI: Failed to authenticate user. Error: "' . strip_tags($response['body']) . '"');
  }
  $this->auth_token = $auth_token['Auth'];
}