du9757 2014-04-12 19:29
浏览 50
已采纳

谷歌api授权很长一段时间

I'm trying to make some script, that will automatically add an event from Joomla add new page to a Google calendar. I made it with this code and it even works, but only for a small amount of time.

I maybe misunderstood, how the Google authorization works. First i must authorize the script to use that calendar and so on. I made it once but after an hour i had to do it again. I would like to authorize it forever or at least for one year (something longer than one hour). And i can't even authorize it during the saving process, because its in saving script, that automatically redirect back to post management.

One more thing to mention: there is an redirect uri and i set it on index.php of administration pages. Not sure if its a problem, but it shouldn't because news are added at index.php?...... so it's still the same page.

But it all works for an hour (or something around that) so i believe that problem is not redirect uri. THX

here is my code (its copy pasted from some google api example page):

        require_once "google-api-php-client/src/Google_Client.php";
        require_once "google-api-phpclient/src/contrib/Google_CalendarService.php";

        session_start();

        $client = new Google_Client();
        $client->setApplicationName("Add Google Calendar Entries");

        $client->setClientId('####');
        $client->setClientSecret('####');
        $client->setRedirectUri('####');
        $client->setDeveloperKey('####');
        $cal = new Google_CalendarService($client);

        if (isset($_GET['logout'])) {
          unset($_SESSION['token']);
        }

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

        if (isset($_SESSION['token'])) {
          $client->setAccessToken($_SESSION['token']);
        }

        if ($client->getAccessToken()) {
          $calList = $cal->calendarList->listCalendarList();

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

        $event = new Google_Event();
        // some calendar adding stuff

        } else {
          $authUrl = $client->createAuthUrl();
          print "<a class='login' href='$authUrl'>Authorize!</a>";
        }

        // Google Calendar API END
    }

Just to summarize: I want my authorization to work longer and maybe there is a mistake in my code or in my understanding of how that works.

  • 写回答

1条回答 默认 最新

  • dongliao8069 2014-04-13 19:42
    关注

    The key piece to get long term access if to request "offline" access from the user when you request authorization.

    $client->setAccessType('offline');
    $authUrl = $client->createAuthUrl();
    

    This should return a refresh token as well as the current session token. You MUST save this refresh token. Even if they authorize the app again later, the refresh token will NOT be sent again. (And on that note, if they are already authorized and you have them authorize again after adding offline access, sometimes you won't get the refresh token.)

    Then you can do a check like the following to keep them logged in:

    $client->setAccessToken($site->token);
    
    if($client->isAccessTokenExpired()) {
        $NewAccessToken = json_decode($client->getAccessToken());
        $client->refreshToken($NewAccessToken->refresh_token);
    }
    

    Finally, I should add that you will need to save the accessToken to the database instead of just the session to actually make it persist over different sessions. Hopefully that was obvious already.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?