douzun4443 2018-01-08 17:56
浏览 69
已采纳

为什么我无法通过PHP从Google OAuth API获取数据?

I have a login script created which allows you to login through the Google API. Everything works fine and the redirect back to my website does also work.

Here is my script for the oauth callback:

<?php
require_once 'google-api-php-client-2.2.1/vendor/autoload.php';
session_start();

if (isset ($_GET["error"])) {

    header('Location: #');

} 

if (isset ($_GET["code"])) {

    $client = new Google_Client();  
    $client->authenticate($_GET['code']);
    $_SESSION['g_access_token'] = $client->getAccessToken();    


    $redirect = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset ($_SESSION["g_access_token"])) {

    $plus = new Google_Service_Plus($client);
    $client->setAccesToken($_SESSION["g_access_token"]);
    $me = $plus->people->get('me');

    $_SESSION["unid"] = $me["id"];
    $_SESSION["g_name"] = $me["displayName"];
    $_SESSION["email"] = $me["emails"][0]["value"];
    $_SESSION["g_profile_image_url"] = $me["image"]["url"];
    $_SESSION["g_cover_image_url"] = $me["cover"]["coverPhoto"]["url"];
    $_SESSION["g_profile_url"] = $me["url"];
}
?>

I dont get any error, but the session variables are still undefined. Where is the problem? I just read and watched lots of tutorials, but still dont get it. Maybe somebody can help me here!

Thank you :)

EDIT

I just debugged my code and now I know that the Session Variable 'g_access_token' is empty after the login. So the main reason why it doesnt work should be the authentication. But I still dont know how to solve that...

  • 写回答

1条回答 默认 最新

  • douhuigan8063 2018-01-10 00:47
    关注

    I've just come from dealing successfully with Google OAuth API for PHP. So far, your code doesn't seem broke. Anyhow, it'd be nice to check your config file from which you get the class new Google_Client().

    As that file is not posted in your question, i recommend you to follow this one, and call it through require_once to whenever you need it:

    <?php
        session_start();
        require_once "GoogleAPI/vendor/autoload.php";
        $gClient = new Google_Client();
        $gClient->setClientId("your_client_id_comes_here");
        $gClient->setClientSecret("your_client_secret");
        $gClient->setApplicationName("CPI Login Tutorial");
        $gClient->setRedirectUri("your_authorized_uri.php");
    //addScope sets the permissions you'll have in your app, whether it is only for displaying them or to handle as you desire (e.g.: store account's information, such as email givenName and so on, into your own db
        $gClient->addScope("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me");
    ?>
    

    Also, don't you forget to enable the Google + API in your Google console. Finally, check that in your credentials, the "Authorized redirect URIs" contains the redirect uri you want the google api to callback once the user is authenticated.

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

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了