Предпринятые шаги:
- Проверенный URI перенаправления: убедитесь, что URI перенаправления в консоли Google API соответствует один, используемый в приложение.
- Проверены проблемы CORS: подтверждено, что клиенту API Google разрешено перенаправление на компьютер моего товарища по команде, и проверены журналы консоли браузера на наличие ошибок, связанных с CORS.
- Проверенные разрешения учетной записи Google: проверяется, имеет ли используемая учетная запись Google необходимые разрешения для приложения OAuth.
загрузить гугл конфигурация
Код: Выделить всё
function loadGoogleLoginInfo($clientId, $clientSecret, $redirectUri) {
// Create Client Request to access Google API
$client = new Google_Client();
$client->setApplicationName("PHP Google OAuth Login Example");
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->addScope("https://www.googleapis.com/auth/userinfo.profile");
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
return $client;
}
Переменная и конструктор
Код: Выделить всё
private $clientId = '[variable]';
private $clientSecret = '[variable]';
private $redirectUri = 'https://localhost/source_web/ctm/login';
private $client;
private $objOAuthService;
private $userData = array();
public function __construct()
{
parent::__construct();
// $this->load->helper('google');
$this->Model = $this->M_myweb->set_table('customer');
// Load google auth config
$this->client = loadGoogleLoginInfo($this->clientId, $this->clientSecret, $this->redirectUri);
// Send Client Request
$this->objOAuthService = new Google_Service_Oauth2($this->client);
}
Код: Выделить всё
public function login()
{
// Add Access Token to Session
if (isset($_GET['code'])) {
$this->client->authenticate($_GET['code']);
$_SESSION['access_token'] = $this->client->getAccessToken();
header('Location: ' . filter_var($this->redirectUri, FILTER_SANITIZE_URL));
}
// Set Access Token to make Request
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$this->client->setAccessToken($_SESSION['access_token']);
}
// Get User Data from Google and store them in $data
if ($this->client->getAccessToken()) {
$this->userData = $this->objOAuthService->userinfo->get();
} else {
$authUrl = $this->client->createAuthUrl();
$this->data['authUrl'] = $authUrl;
}
if (!isEmpty($this->userData)) {
$flag = $this->Model->set('oauthUid', $this->userData['id'])->get();
$res = $this->register($flag);
$user = array(
'uId' => $flag->id,
'firstName' => $this->userData['given_name'],
'lastName' => $this->userData['family_name'],
'avatar' => $this->userData['picture'],
'is_admin' => false,
'logged_in' => true,
'token' => $_SESSION['access_token']['access_token']
);
if (!$res) {
$user['role'] = 0;
} else {
$user['role'] = $flag->role;
}
$_SESSION['system'] = !isEmpty($user) ? (object) $user : null;
}
$this->data['userData'] = $this->userData;
$this->data['title'] = 'Login';
$this->data['subview'] = 'customer/auth/V_home';
$this->load->view('default/_main_page', $this->data);
}
Код: Выделить всё
private function register($flag)
{
if (!isEmpty($flag)) {
$user = array(
'avatar' => $this->userData['picture'],
'firstName' => $this->userData['given_name'],
'lastName' => $this->userData['family_name'],
'updateAt' => date('Y-m-d H:i:s')
);
$this->Model->sets($user)->setPrimary($this->userData['id'])->save();
return true;
} else {
$user = array(
'oauthUid' => $this->userData['id'],
'role' => 0,
'account' => $this->userData['email'],
'avatar' => $this->userData['picture'],
'firstName' => $this->userData['given_name'],
'lastName' => $this->userData['family_name'],
'createAt' => date('Y-m-d H:i:s')
);
$this->Model->sets($user)->save();
return false;
}
}
- Я уже спрашивал в чатGPT и подумал, что это предложение, но оно не сработало.
- Приложение работает правильно на моем локальном компьютере.
- Проблема возникает только на компьютере моего товарища по команде.
- URI перенаправления в консоли Google API настроен правильно.
[*]Некоторые проблемы со средой
[*]Некоторые проблемы с конфигурацией
Подробнее здесь: https://stackoverflow.com/questions/793 ... machine-bu