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

Pet*_*ble 1 javascript php rest oauth google-api

我需要构建一个连接到Google日历并获取指定事件集的小型Web应用程序.我已经阅读了文档,我了解到我需要对用户进行身份验证,这是我在Google Developer Console上创建的应用程序.

我正在尝试使用弹出窗口实现身份验证(我正在使用fancybox).这是<div>登录数据的来源:<div id="login-popup" style="display:none"></div>

这是我用来调用auth.php文件的JS代码:

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

最后,这是将GET请求发送到Google API的PHP代码:

$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);
Run Code Online (Sandbox Code Playgroud)

所以这个弹出窗口会在此之后出现,因为它应该是:

在此输入图像描述

但是,在我输入凭据后,它会将我重新搜索到Google的登录网站(https://accounts.google.com/ServiceLoginAuth).

我的问题:

我需要能够从这个弹出窗口中获取数据,以获取我以后可以在Calendar API调用中使用的访问令牌.我需要做些什么来实现这一目标?

acu*_*joe 5

您被重定向的原因是因为您实际上是将Google的页面加载到iframe中.输入时会调用通常在提交时登录页面上调用的所有内容.所以基本上你只是打开一个谷歌在你的网站内的窗口.

实现这一点的正确方法是使用谷歌的oauth API.(https://developers.google.com/accounts/docs/OAuth2或更具体地说,https://developers.google.com/accounts/docs/OAuth2Login)

您需要注册项目并获取开发人员/ API密钥.

谷歌文档非常全面,所以你应该能够很容易地遵循它们!