dongmu1951 2014-10-27 15:42
浏览 49
已采纳

创建Google登录页面以访问服务

I need to build a small web application which connects to Google Calendars and fetches a specified event set. I've read the documentation, and I understand that I need to authenticate the user to my app which I created on the Google Developer console.

I'm trying to implement an authentication with a popup window (I'm using fancybox for this). This is the <div> in which the login data goes: <div id="login-popup" style="display:none"></div>

This is the JS code I'm using to call the auth.php file:

    $.get("/auth.php", function(result) {
            $('#login-popup').append(result);
            $.fancybox({
                href: "#login-popup",
                autoSize: true,
                width: "60%",
                fitToView: true
            });
        });

And finally this is the PHP code that sends the GET request to Google's API:

$ch = curl_init();
if($ch) {
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $query);
    $result = curl_exec($ch);

    curl_close($ch);
}
else {
    throw new RuntimeException("Hiba történt a távoli adatok elérése közben!");
}
echo json_encode($result);

So this popup comes up after this, as it's supposed to be:

enter image description here

After I enter my credentials however, it redirrects me to Google's login website (https://accounts.google.com/ServiceLoginAuth).

My problem:

I need to be able to get data back from this popup window to get the access token I can later use on my Calendar API call. What do I need do to achieve this?

  • 写回答

1条回答 默认 最新

  • doxd96148 2014-10-27 15:57
    关注

    The reason you are being redirected is because you are essentially loading google's page into an iframe. Everything that is normally called on their login page on submit is called when you input. So basically you're just opening a window to google within your website.

    The correct way to implement this would be to use google's oauth API. (https://developers.google.com/accounts/docs/OAuth2 or more specifically, https://developers.google.com/accounts/docs/OAuth2Login)

    You'll need to register a project and get a developer/API key.

    The google docs are very comprehensive, so you should be able to follow them pretty easily!

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

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容