我遵循了本教程(http://blog.jachobsen.com/2013/08/10/google-oauth2-in-android-with-rails-backend/)创建oauth2身份验证的android部分。最后,它提供了服务器后端代码的Rails部分,但我对Rails不太熟悉,所以不太确定如何做到这一点
有人知道我如何用PHP创建类似的东西吗?我看过这个https://code.google.com/p/google-api-php-client/但我无法编辑代码以获得访问令牌,检查是否有效,然后返回API密钥。
非常感谢Daniel
您正在查看的PHP示例不是Google+/authroization的最新示例。您应该从Google+文档中的最新内容开始:
Google+PHP Quickstart
quickstart向您展示了如何授权客户端并将凭据传递给您的PHP后端以进行API调用。
如果你的服务器不运行Phar,你可以从你链接的PHP客户端库页面开始使用示例代码,并可以更新它,根据传递给你的Android应用程序的代码执行代码交换,或者用网络上的访问令牌/授权码授权用户。
以下示例执行web的代码交换(正如在quickstart示例中的/connect端点中所做的那样):
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setDeveloperKey('YOUR_SIMPLE_API_KEY');
$plus = new Google_PlusService($client);
if (isset($_GET['webcode'])) {
$client->setRedirectUri('postmessage');
$client->authenticate($_GET['webcode']);
$_SESSION['token'] = $client->getAccessToken();
$activities = $plus->activities->listActivities('me', 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';
}
要查看代码的工作情况,您需要从web客户端生成一个代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<span id="signinButton">
<span
class="g-signin"
data-callback="signinCallback"
data-clientid="YOUR_CLIENT_ID"
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-scope="https://www.googleapis.com/auth/plus.login">
</span>
</span>
<table>
<tr>
<th>Code</th><th>ID Token</th><th>Access token</th>
</tr>
<tr>
<td><textarea id="code"></textarea></td>
<td><textarea id="idtok"></textarea></td>
<td><textarea id="atok"></textarea></td>
</tr>
</table>
<script type="text/javascript">
(function () {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/client:plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
function signinCallback(resp) {
console.log(resp);
document.getElementById('code').value = resp.code;
document.getElementById('idtok').value = resp.id_token;
document.getElementById('atok').value = resp.access_token;
}
</script>
</body>
</html>
然后将其作为web代码参数传递给php脚本。我创建了一个演示,运行在where sgus.com/phptest/sttackdemo.php上,您可以从http://wheresgus.com/phptest/gencode.html
GET的一个示例如下:
http://wheresgus.com/phptest/stackdemo.php?webcode=4/ajfCXQiZo-zRBAJGktP_eSYRha2s.YiEFJjWUiW4bEnp6UAPFm0GQNJMGhgI
在实践中,您应该通过HTTPS张贴您的代码;但为了让你开始,希望这能有所帮助。
对于您的移动客户端,您应该能够使用以下ID令牌进行验证:
- 安全地将ID令牌传递给您的应用程序
- 验证令牌是否有效并且属于正确的应用程序
- 使用子字段识别用户
下面的代码显示了同一应用程序中的一个函数,该函数验证令牌并使用简单的API访问来为登录用户执行activity.list:
if (isset($_GET['idtoken'])) {
$attributes = $client->verifyIdToken($_GET['idtoken'], CLIENT_ID)
->getAttributes();
$gplus_id = $attributes["payload"]["sub"];
// At a minimum, make sure the token was for this app.
if ($attributes["payload"]["aud"] == $client->getClientId()){
$activities = $plus->activities->listActivities($gplus_id, 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';
}else{
print 'Authorization failed.';
}
}
此处提供完整代码。