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

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

我正在尝试使用弹出窗口实现身份验证(我正在使用 fancybox 为此)。 这是登录数据所在的&lt; div&gt; </ code>:&lt; div id =“login-popup”style =“display:none”&gt;&lt; / div&gt; </ code> </ p>

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

  $ .get(“/  auth.php“,function(result){
$('#login-popup')。append(result);
$ .fancybox({
href:”#login-popup“,
autoSize:true ,
width:“60%”,
fitToView:true
});
});
</ code> </ pre>

最后这是PHP代码 将GET请求发送到Google的API:</ p>

  $ ch = curl_init(); 
if($ ch){
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Content- 键入: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(“Hibatörténtatávolia datokeléréseközben!“);
}
echo json_encode($ result);
</ code> </ pre>

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

</ p>

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

我的问题:</ p>

我需要能够从此弹出窗口获取数据以获取我以后可以在我的日历上使用的访问令牌 API调用。 我需要做些什么来实现这个目标?</ p>
</ div>

展开原文

原文

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个回答



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

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

您需要注册项目并获取开发人员/ API密钥。 </ p>

Google文档非常全面,因此您应该可以非常轻松地关注它们。</ p>
</ div>

展开原文

原文

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!

立即提问
相关内容推荐