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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)