php – google登录oauth2每次重新询问访问权限
发布时间:2020-12-13 18:05:04 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用google oauth2 api登录系统.它几乎正常工作,用户可以访问我的Web应用程序,读取信息并连接用户.问题是,一旦他们离开或更改浏览器,回到我的网站,他们再次被要求提供访问权限. 我不需要离线访问,因为除了签入用户之外我没有使用任何API调用.无论
我正在尝试使用google oauth2 api登录系统.它几乎正常工作,用户可以访问我的Web应用程序,读取信息并连接用户.问题是,一旦他们离开或更改浏览器,回到我的网站,他们再次被要求提供访问权限.
我不需要离线访问,因为除了签入用户之外我没有使用任何API调用.无论如何我被困住了,需要一些帮助! 我正在使用谷歌php库(https://code.google.com/p/google-api-php-client/wiki/OAuth2),我甚至将ApprovalPrompt设置为auto.仍然没有运气. 我的代码: public function googleLogin() { $this->set('connect',"Google login"); $client = new apiClient(); $client->setApprovalPrompt('auto'); $client->setApplicationName("Authentication"); $client->setClientId(G_OAUTH2_APP_ID); $client->setClientSecret(G_OAUTH2_SECRET); $client->setRedirectUri(G_REDIRECT_URI); $client->setDeveloperKey(G_DEV_KEY); $oauth2 = new apiOauth2Service($client); if (isset($_GET['code'])) { $client->authenticate(); $_SESSION['token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect,FILTER_SANITIZE_URL)); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } if (isset($_REQUEST['logout'])) { unset($_SESSION['token']); $client->revokeToken(); } //print_r($client->getAccessToken()); exit; if ($client->getAccessToken()) { $user = $oauth2->Guserinfo->get(); // These fields are currently filtered through the PHP sanitize filters. // See http://www.php.net/manual/en/filter.filters.sanitize.php //$email = filter_var($user['email'],FILTER_SANITIZE_EMAIL); //$img = filter_var($user['picture'],FILTER_VALIDATE_URL); // The access token may have been updated lazily. $_SESSION['token'] = $client->getAccessToken(); //do stuff with user data $data = $this->linkUser($user,"google"); if ($data['state']=="createUser") { $this->set("data",$data); } } else { $authUrl = $client->createAuthUrl(); header("Location: " . $authUrl); } } 编辑:
关于另一个问题:
Google API – forces to Grant Permission Every time
有人指出你可以这样做: $客户端 – > setApprovalPrompt( ‘自动’); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |