function gauth_client_get in Google Auth 7
Same name and namespace in other branches
- 7.2 gauth.module \gauth_client_get()
Get Google_client object of an account.
Parameters
string $account_id: Name or id of the account which will be used to create google client. Account array can also be passed with all parameters
bool $by_name: Set False if passing account id and True for account name
2 calls to gauth_client_get()
- gauth_account_revoke_token in ./
gauth.module - Unauthenticate an account.
- _gauth_read_scope_info in ./
gauth.module - Helper function which reads the installed library and discovery api to build cache for scopes and names.
File
- ./
gauth.module, line 513 - Google Auth Api for drupal.
Code
function gauth_client_get($account_id = NULL, $by_name = TRUE) {
$info = libraries_load('google-api-php-client');
if (!$info['loaded']) {
drupal_set_message(t("Can't create client object as library is missing check Status report or Readme for requirements"), 'error');
return FALSE;
}
if ($account_id == NULL) {
$client = new Google_Client();
}
else {
if (is_array($account_id)) {
$account = $account_id;
}
else {
$account = gauth_account_load($account_id, $by_name);
}
$client = new Google_Client();
$client
->setClientId($account['client_id']);
$client
->setAccessToken($account['access_token']);
$client
->setAccessType($account['access_type']);
$client
->setClientSecret($account['client_secret']);
$client
->setDeveloperKey($account['developer_key']);
if ($account['access_type'] == 'offline') {
$client
->setApprovalPrompt('force');
}
}
$client
->setApplicationName("Google OAuth2");
$client
->setRedirectUri(gauth_callback_url());
if ($account_id == NULL) {
return $client;
}
if ($client
->isAccessTokenExpired()) {
if ($client
->getRefreshToken() != '') {
// Access Type is Offline
$client
->refreshToken($client
->getRefreshToken());
$token = $client
->getAccessToken();
$account['access_token'] = json_encode($token);
drupal_write_record('gauth_accounts', $account, 'id');
}
else {
$names = explode('|', $account['name']);
if ($names[count($names) - 1] == $GLOBALS['user']->uid) {
// Services account
gauth_account_authenticate($account_id, $by_name);
}
else {
$client
->revokeToken();
$account['is_authenticated'] = FALSE;
$account['access_token'] = '';
drupal_write_record('gauth_accounts', $account, 'id');
drupal_set_message(t('Access token is expired. If you are admin then you need to authenticate again. Consider configuring access type to offline.'));
}
}
}
return $client;
}