未经授权的谷歌访问令牌

Rom*_*lin 5 php google-analytics access-token

我正在开发用于从谷歌分析帐户获取额外数据的模块。为了获得这些数据,谷歌需要 access_token。这是我到目前为止所管理的

if (isset($_GET['code'])) {
    // try to get an access token
    $code = $_GET['code'];
    $url = 'https://accounts.google.com/o/oauth2/token';
    $params = array(
        "code" => $code,
        "client_id" => "559825975819-881lg83vs8feo70v5unqa8kfoijuvfnn.apps.googleusercontent.com",
        "client_secret" => "vj4UNNItAJocX4RkNaD_3DQ4",
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "access_type" => "offline",
        "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);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
 $data = (json_decode($output, true));


 $access_token_var = $data['access_token'];

  echo $access_token_var;

} else {

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

    $params = array(
        "response_type" => "code",
        "client_id" => "559825975819-881lg83vs8feo70v5unqa8kfoijuvfnn.apps.googleusercontent.com",
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "scope" => "https://www.googleapis.com/auth/analytics",
        "access_type" => "offline",
        "approval_prompt" => "force"
    );

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

    header("Location: " . $request_to);
}
Run Code Online (Sandbox Code Playgroud)

我得到了 access_token,它在需要的变量中回响。但是我想在后台进程中获取分析附加数据(例如,在客户下订单和点击订单按钮时​​),但是每次我需要新的 access_token 时,我都需要使用我的 google 帐户进行授权,因此,网站上的每个客户都需要为此,尽管我设置了“access_type”=>“离线”。怎么了?还是我的 API 应用程序有问题?

Geo*_*Myl 7

访问令牌在短时间内过期。每次访问令牌过期时,您都需要刷新它。

https://developers.google.com/identity/protocols/OAuth2WebServer

访问令牌会定期过期。如果您请求离线访问与令牌关联的作用域,您可以刷新访问令牌而不提示用户授予权限(包括用户不在时)。

您无需刷新即可自行访问令牌。您可以使用 Goggle php 库https://github.com/googleapis/google-api-php-client以便它可以自动刷新令牌。这是从 Google 网站上获取的示例。

$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
  $drive = new Google_Service_Drive($client);
  $files = $drive->files->listFiles(array())->getItems();
  echo json_encode($files);
} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Run Code Online (Sandbox Code Playgroud)

如果您不想或不能使用 Google Php 库,当您将授权代码交换为访问令牌时,Google 的授权服务器会返回一个刷新令牌。然后,如果访问令牌过期(或在任何其他时间),您可以使用刷新令牌来获取新的访问令牌。您可以使用通过 curl 获取访问令牌的相同方式,但使用不同的参数。

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded

client_id=<your_client_id>&
client_secret=<your_client_secret>&
refresh_token=<refresh_token>&
grant_type=refresh_token
Run Code Online (Sandbox Code Playgroud)