duanduo7400 2014-10-09 12:34
浏览 38

无法刷新令牌

When I first enter my index.php from my web browser I get redirected to Google to aprove that my applications right to access my account. Then I want the application to save the refresh token and never ask me again. But when I try to access index.php trhou another browser it ask me the same thing again. I want to access index.php as a cronjob so it must not ask for an google account each time.

Here is my code:

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);

if (isset($_REQUEST['logout'])) {
    write_token("");
}

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    write_token($client->getAccessToken());
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (strlen(read_token()) > 1) {
    $client->setAccessToken(read_token());
    if ($client->isAccessTokenExpired()) {
        write_token("");
    }
} elseif(!isset($_GET['code'])) { // The IF NOT CODE prevent loop when refreshing token
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl); // Update token if expired
}

Thanks!

/ Erik

  • 写回答

1条回答 默认 最新

  • doujingya1166 2014-10-10 21:03
    关注

    The prompt can be skipped for users that have already authorized the application before using the following code. See this answer for more information.

    To make this work in a cron job, when there is no user signed into the browser, you'll need to request offline access and use a refresh token. See this answer for more information.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?