使用google api时如何设置refresh_token?

时间:2014-12-02 15:36:56

标签: php google-api google-api-php-client

我正在尝试学习如何使用google api更改日历上的活动。服务器是将基于数据库中的信息更新日历的用户。实际上不需要用户交互。

问题是我在获取/使用刷新令牌时遇到问题。我单击添加到页面的“连接我”链接,它给出了一个错误:

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_request", "error_description" : "Missing required parameter: refresh_token" }

我有tried setting the refresh token this way以及类似的方法,但它们似乎都没有用,这让我觉得我正在错误地实现它们。

当我打印出$_SESSION['access_token']变量时,它没有显示refresh_token

{"access_token":"token","token_type":"Bearer","expires_in":3599,"created":1417534503}

以下是我用来授权没有refresh_token部分的'用户'的功能(基于示例):

function googleAuth(){

    $client_id = 'myclientid';
    $client_secret = 'myclientsecret';
    $redirect_uri = 'redirecturi';
    $client = new Google_Client();
    $client->setAccessType('offline');
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->setScopes('https://www.googleapis.com/auth/calendar');

    /************************************************
    If we're logging out we just need to clear our
    local access token in this case
    ************************************************/
    if (isset($_REQUEST['logout'])) {
    unset($_SESSION['access_token']);
    }
    /************************************************
    If we have a code back from the OAuth 2.0 flow,
    we need to exchange that with the authenticate()
    function. We store the resultant access token
    bundle in the session, and redirect to ourself.
    ************************************************/
    if (isset($_GET['code'])) {
        $resp = $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();

        $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

    }
    /************************************************
    If we have an access token, we can make
    requests, else we generate an authentication URL.
    ************************************************/
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
    } else {
        $authUrl = $client->createAuthUrl();
    }

    if (isset($authUrl)) {
        echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
    }

    return $client;
}

以下是我用于将事件添加到日历的代码:

function addEvent($title, $location, $startTime, $stopTime, $client){

    $event = new Google_Service_Calendar_Event();
    $start = new Google_Service_Calendar_EventDateTime();

    $event->setSummary($title);
    $event->setLocation($location);

    $start->setDateTime($startTime);

    $end = new Google_Service_Calendar_EventDateTime();

    $end->setDateTime($stopTime);

    $event->setStart($start);
    $event->setEnd($end);

    $atendee = new Google_Service_Calendar_EventAttendee();
    $atendee->setEmail('someGuy@someDomain.com');

    $atendees = array($atendee);

    $event->attendees = $atendees;

    $service = new Google_Service_Calendar($client);

    $event_id = $service->events->insert('primary', $event);

}

如何设置缺少的参数:“refresh_token”?代码结构是否存在问题?我查看了文档,我愿意再看一些,但是如果有人可以帮助解释如何做到这一点,那将是惊人的。谢谢!

1 个答案:

答案 0 :(得分:5)

检查令牌是否有刷新令牌(如果您请求离线访问,刷新令牌将首次与访问令牌一起发送)。

像这样的东西

 $token = $client->getAccessToken();
 $authObj = json_decode($token);
 if(isset($authObj->refresh_token)) {
     save_refresh_token($authObj->refresh_token);
 }

save_refresh_token将刷新令牌保存在某处(db)。

然后您可以通过以下方式检查令牌是否已过期:

 $client->isAccessTokenExpired()

如果是,您可以使用以下命令进行更新:

$client->refreshToken($your_saved_refresh_token);

然后将新的访问令牌设置为会话:

 $_SESSION['access_token'] = $client->getAccessToken();