持久 Google OpenID+OAuth?

发布于 2024-11-08 07:24:39 字数 334 浏览 0 评论 0原文

我正在开发一个网络应用程序,需要频繁访问 Google 数据 API,因此我决定采用“OAuth with Federated Login(混合协议)”方法让用户登录该应用程序。我让 http://googlecodesamples.com/hybrid/ 正常工作(在对 PHP 5.3 兼容性进行一些调整之后),并且能够获得访问令牌。下一步是什么?如何使用此访问令牌?

看来我需要为用户创建一个本地会话来浏览应用程序的其余部分。这是否需要完全独立于 Google 登录,或者您将如何处理?

相关:该应用程序还需要一个 REST API,我计划使用 OAuth。关于如何将其与实际应用程序的身份验证结合起来有什么建议吗?

I'm working on a web app that will require somewhat frequent access to Google Data APIs, so I decided to go with the "OAuth with Federated Login (Hybrid Protocol)" method for users to log into the app. I got the http://googlecodesamples.com/hybrid/ working (after some tweaks for PHP 5.3 compatibility), and am able to get an Access Token. What's the next step? How do I use this access token?

It seems like I'll need to create a local session for the user to browse the rest of the app. Will this need to be completely independent of the Google login, or how would you handle that?

Relevant: this application also needs a REST API, for which I was planning to use OAuth. Any recommendation on how to tie this in with authentication for the actual app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-11-15 07:24:39

我正在使用 PHP LightOpenID 库(参见 gitorious)。它为我们处理所有身份验证流程。您无需担心令牌之类的事情。

这里是我显示“使用 Google 登录”链接的页面:

<?php
require_once 'openid.php';
$openid = new LightOpenID;

$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
$openid->returnUrl = 'http://my-website.com/landing-login.php'
?>

<a href="<?php echo $openid->authUrl() ?>">Login with Google</a>

当点击该链接时,会出现一个 Google 页面,要求他进行身份验证和/或授权您检索他的电子邮件。

然后他将被重定向到登陆页面$openid->returnUrl。该页面的代码应该是:

<?php
require_once 'openid.php';
$openid = new LightOpenID;

if ($openid->mode) {
    if ($openid->mode == 'cancel') {
        // User has canceled authentication
    } elseif($openid->validate()) {
        // Yeah !
        $data = $openid->getAttributes();
        $email = $data['contact/email'];
    } else {
        // The user has not logged in via Google
    }
} else {
    // The user does not come from the link of the first page
}
?>

如果您想从用户那里检索更多信息,您必须将它们添加到第一页的 $openid->required 中。例如:

$openid->required = array(
  'contact/email',
  'namePerson/first',
  'namePerson/last'
);

如果用户接受,您将可以在第二页中获取他的名字和姓氏:

$name = $data['namePerson/first'] . " " . $data['namePerson/last'];

然后,对于 Oauth 部分,您可以按照 此 LightOpenID 问题

I am using the PHP LightOpenID library (see on gitorious) for that. It handles all the authentication flow for us. You don't need to bother about token and stuff.

Here the page where I display the "Login with Google" link :

<?php
require_once 'openid.php';
$openid = new LightOpenID;

$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
$openid->returnUrl = 'http://my-website.com/landing-login.php'
?>

<a href="<?php echo $openid->authUrl() ?>">Login with Google</a>

When the click on the link, a Google page will appear ask him to authenticate and/or authorize you to retrieve his email.

Then he will be redirect to the landing page $openid->returnUrl. The code for that page should be :

<?php
require_once 'openid.php';
$openid = new LightOpenID;

if ($openid->mode) {
    if ($openid->mode == 'cancel') {
        // User has canceled authentication
    } elseif($openid->validate()) {
        // Yeah !
        $data = $openid->getAttributes();
        $email = $data['contact/email'];
    } else {
        // The user has not logged in via Google
    }
} else {
    // The user does not come from the link of the first page
}
?>

If you want to retrieve more info from the user, you have to add them to $openid->required in the first page. For instance :

$openid->required = array(
  'contact/email',
  'namePerson/first',
  'namePerson/last'
);

will let you, if the user accepts it, to get his first and last names as well in the second page :

$name = $data['namePerson/first'] . " " . $data['namePerson/last'];

Then, for the Oauth part, you can follow the instructions of this LightOpenID issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文