douqufan9148 2017-10-07 21:14
浏览 69
已采纳

保持谷歌当前用户跨路线记录

I try to make an web application which use google api for the user system.

Actually, I can only connect my google account to one route, here it is :

$app->get('/home', function () use($client,$app){
    if(isset($_GET['code'])){
        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
    }
    if(!isset($_SESSION['token'])){
        $url = $client->createAuthUrl();
        $output = '<a href="'.$url.'">Se connecter </a>';
    } else {
        $client->setAccessToken($_SESSION['token']);
        $token = json_decode($_SESSION['token']['access_token']);
        $output = "";
        require "../view/planning.php";
    }

    return $output;
});

Here is my $client :

$client = new Google_Client();
$client->setApplicationName("Application de test");
$client->setClientId(MY_CLIENT_ID);
$client->setClientSecret(MY_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/calendar.readonly');
$client->addScope('https://www.googleapis.com/auth/userinfo.email');
$client->setRedirectUri(MY_REDIRECT_URI);
$client->setAccessType('online');

And my question is : how I can keep the current client to the whole application ? For exemple, if I have this route :

$app->get('/accueil', function () use($client){
  if(isset($_GET['code'])){
      $client->authenticate($_GET['code']);
      $_SESSION['token'] = $client->getAccessToken();
  }
  if(!isset($_SESSION['token'])){
      $url = $client->createAuthUrl();
      $output = '<a href="'.$url.'">Se connecter </a>';
  } else {
      $client->setAccessToken($_SESSION['token']);
      $token = json_decode($_SESSION['token']['access_token']);
      require ('../view/accueil.php');
      $output = "";
  }
  return $output;
});

But this route not working, it show me the link to login. I understand that the application cannot get 'code' from the url, because there is nothing.

I also tried to keep the 'code' like this :

if(isset($_GET['code']) || isset($_SESSION['code'])){
    $client->authenticate($_GET['code']);
    $_SESSION['code'] = $_GET['code'];
    $_SESSION['token'] = $client->getAccessToken();
}

How can I keep the user logged ?

  • 写回答

1条回答 默认 最新

  • dsfhe34889 2017-10-08 13:24
    关注

    Well, after a long night, I find the solution.

    It seemes like the redirect on 127.0.0.1 is different than redirect to localhost, so session variables are reset.

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?