douhuang9886 2015-08-24 21:32
浏览 29

使用Google API登录PHP

I'm currently building a web app, and I want to log in to the app via Google, much like many other websites (including SO) do. However, I'm having difficulty finding a cohesive tutorial that is current and actually works. I was hoping that you, SO, would have some helpful material to assist me. If you guys have any actually useful and cohesive instructions or links to provide, that would be fantastic. Thanks in advance, SO!

EDIT:

So far, I've got a tutorial's code up and running, but whenever I submit it, it seems like it is unable to get an access token. Here ya go:

<?php
include_once("config.php");
include_once("includes/functions.php");

//print_r($_GET);die;
echo("output test ");
    if(isset($_REQUEST['code'])){
    $gClient->authenticate();
    $_SESSION['token'] = $gClient->getAccessToken();
    header('Location: ' . filter_var($redirect_url, FILTER_SANITIZE_URL));
}

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

if ($gClient->getAccessToken()) {
    $userProfile = $google_oauthV2->userinfo->get();
    echo("yep ");
    //DB Insert
    $gUser = new Users();
         $gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],$userProfile['locale'],$userProfile['link'],$userProfile['picture']);
    $_SESSION['google_data'] = $userProfile; // Storing Google User Data in Session
    header("location: account.php");
    $_SESSION['token'] = $gClient->getAccessToken();
} else {
    $authUrl = $gClient->createAuthUrl();
    echo("nope ");
}

if(isset($authUrl)) {
    echo '<a href="'.$authUrl.'"><img src="images/glogin.png" alt=""/> </a>';
} else {
    echo '<a href="logout.php?logout">Logout</a>';
}

?>

I have no idea what's really going on, so any help is appreciated. Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么