Google oAuth可在localhost和Heroku应用中运行,但在部署时无法正常运行

时间:2018-06-16 07:03:17

标签: php oauth web-hosting google-login

这已经困扰了我好几天了,我无法在网上找到解决方案(也许我不知道该搜索什么)。我想在php中使用Google进行简单的登录。这是我的config.php:

<?php
session_start();
require_once "GoogleAPI/vendor/autoload.php";
$gClient = new Google_Client();
$gClient->setClientId("unique_client_id.apps.googleusercontent.com");
$gClient->setClientSecret("unique_secret");
$gClient->setApplicationName("Nurture");
$gClient->setRedirectUri("http://nurture1729.esy.es/g-callback.php");
$gClient->addScope("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email");
?>

以下是我的g-callback.php的代码(授权后回调):

<?php
require_once "config.php";
if (isset($_SESSION['access_token']))
    $gClient->setAccessToken($_SESSION['access_token']);
else if (isset($_GET['code'])) {
    $token = $gClient->fetchAccessTokenWithAuthCode($_GET['code']);
    $_SESSION['access_token'] = $token;
} else {
    header('Location: login.php');
    exit();
}

$oAuth = new Google_Service_Oauth2($gClient);
$userData = $oAuth->userinfo_v2_me->get();

$_SESSION['id'] = $userData['id'];
$_SESSION['email'] = $userData['email'];
$_SESSION['familyName'] = $userData['familyName'];
$_SESSION['givenName'] = $userData['givenName'];
header('Location: PCM_2018/application.php');
exit();
?>

问题是,当我在localhost和我制作的heroku应用程序中托管时,此代码工作正常。但是,当回调在hotinger中托管时,回调不会成为标题行。是因为代码还是其他我忽略的东西。我想要的只是将电子邮件ID存储在数据库中,因此任何其他使用谷歌登录存储它的方式也将受到赞赏。谢谢。

0 个答案:

没有答案