D
D
DorF LorS2021-05-24 06:53:54
PHP
DorF LorS, 2021-05-24 06:53:54

How and where should I store the 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
When I go to Google for the first time I get 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"`

How and where should I store the access /refresh token so that the next time it takes it from the session or from somewhere else and does not have to each click on login.

2Q. How can I get access to the access token in the 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);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question