Answer the question
In order to leave comments, you need to log in
How to properly configure Google API OAuth 2.0 authorization in CodeIgniter?
Hello.
During development, it became necessary to authorize users in the Google API via OAuth 2.0. The meaning is this: there is a certain model, and some of its fields must be added to Google Calendar as an event.
At the moment, everything is implemented, it seems to me, far from perfect.
The user is prompted to click on a link of the form /controller_name/gcalendar_initialize/137
where "137" is the id of the object to be added to the calendar, and gcalendar_initialize is the name of the method that starts authorization in the Google API. At the same time, it receives an auth-url, and the Google_Client object and records the id "137" are written to the session, after which the user is redirected to the auth-url:
public function gcalendar_initialize($id) {
...
// записываем в сессию сериализованную версию объекта
$_SESSION['google_api_object'] = serialize($google_api_object);
// также записываем id
$_SESSION['id'] = $id;
// уходим по ссылке авторизации
header ('Location: '.$google_api_object->get_auth_url());
}
/controller_name/finish_authorization_in_gcalendar?code=...
public function finish_authorization_in_google_calendar() {
...
// вытягиваем из сессии сериализованную версию объекта
$google_api_object = unserialize($_SESSION['google_api_object']);
// устанавливаем access token
$google_api_object->set_access_token_in_calendar($this->input->get('code'));
// переходим непосредственно к добавлению объекта в календарь
$created_event = $this->model_name->add_to_google_calendar($google_api_object, $_SESSION['id']);
// возвращаемся на страницу, с которой начали
header ('Location: /index.php/controller_name/?gcalendar_success='.$_SESSION['id']);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question