@DorF_0

Как и где мне хранить access token?

I am trying to authenticate with my google account but having trouble with getting access token and refreshing token. First when I run the code it gave me this ressult
Я пытаюсь войти в google account так чтобы мне не приходилось каждый раз входить в аккаунт.
Когда в первый раз захожу в гугл получаю access and refresh token

`"access_token": "ya29.a0AfH6SMA****",

"expires_in": 3599,
"refresh_token": "1//0clWIneP-7m**,

"scope": "https://www.googleapis.com/auth/spreadsheets",

"token_type": "Bearer"`

Как и где мне хранить access /refresh token чтобы на следующий раз он брал с сессии или откуда то еще и не приходилось каждый раз нажимать на логин.

2Q. Как и мне заполучить доступ к access token'у в response?
if (isset($_GET['code'])) {
    $code = $_GET['code'];
    $url = 'https://accounts.google.com/o/oauth2/token';
    $params = array(
      "code"          => $code,
        "client_id"     => $CLIENT_ID,
        "client_secret" => $CLIENT_SECRET,
        "access_type"   => "offline",
        "redirect_uri"  => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "grant_type"    => "authorization_code",
    );

    $ch = curl_init();
    curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url);
    curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);
    curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    if ($info['http_code'] === 200) {
        header('Content-Type: ' . 'application/x-www-form-urlencoded');
        return $output;
    } else {
        return 'An error happened';
    }
} else {

    $url = "https://accounts.google.com/o/oauth2/auth";

    $params = array(
        "response_type" => "code",
        "client_id" => $CLIENT_ID,
        "access_type"=> "offline",
        "redirect_uri" => $redirect_uri,
        "scope" => $SCOPE,
    );

    $request_to = $url . '?' . http_build_query($params);

    header("Location: " . $request_to);
}
  • Вопрос задан
  • 125 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы