在开始之前,我要声明我对 OpenID 一无所知。我什至不想做 OpenID 的用途,但我想人们会提到它,但这不是我想要的。
我有软件。该软件要求用户在注册时提供其 Steam 用户名。他们不通过 Steam 登录,只是提供他们的用户名,以便其他人知道他们的 Steam 用户名。所以不需要OpenID。
我知道,我可以简单地添加一个文本字段,让他们列出他们的 Steam 用户名,然后就到此为止了。然而,这样做,人们几乎可以输入他们想要的任何蒸汽用户名并完成。相反,我希望能够确认他们的用户名。
理想情况下,应该有一个“验证 Steam 帐户”按钮。人们点击它,它会弹出一个 Steam 登录表单。人们登录,然后 Steam 返回他们的用户名(可能还有一些额外的数据,例如他们的头像)。最好的方法是什么?
需要 OpenID。这就是 Valve 根据他们的情况使用的方法文档.
您没有提到您的应用程序是用什么编写的,所以我只能猜测您是通过网页执行此操作的。在这种情况下,我建议使用LightOpenID图书馆。从这里开始,此示例代码应该能够帮助您入门。
<?php
require 'includes/lightopenid/openid.php';
$_STEAMAPI = "YOURSTEAMAPIKEY";
try
{
$openid = new LightOpenID('http://URL.TO.REDIRECT.TO.AFTER.LOGIN/');
if(!$openid->mode)
{
if(isset($_GET['login']))
{
$openid->identity = 'http://steamcommunity.com/openid/?l=english'; // This is forcing english because it has a weird habit of selecting a random language otherwise
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<input type="image" src="http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_small.png">
</form>
<?php
}
elseif($openid->mode == 'cancel')
{
echo 'User has canceled authentication!';
}
else
{
if($openid->validate())
{
$id = $openid->identity;
// identity is something like: http://steamcommunity.com/openid/id/76561197960435530
// we only care about the unique account ID at the end of the URL.
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
echo "User is logged in (steamID: $matches[1])\n";
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$_STEAMAPI&steamids=$matches[1]";
$json_object= file_get_contents($url);
$json_decoded = json_decode($json_object);
foreach ($json_decoded->response->players as $player)
{
echo "
<br/>Player ID: $player->steamid
<br/>Player Name: $player->personaname
<br/>Profile URL: $player->profileurl
<br/>SmallAvatar: <img src='$player->avatar'/>
<br/>MediumAvatar: <img src='$player->avatarmedium'/>
<br/>LargeAvatar: <img src='$player->avatarfull'/>
";
}
}
else
{
echo "User is not logged in.\n";
}
}
}
catch(ErrorException $e)
{
echo $e->getMessage();
}
?>
使用此功能,它将向用户显示 Steam 登录 ID 按钮。单击后,会将用户重定向到 Steam 社区登录页面。登录后,用户将重定向回您在LightOpenID
构造函数。如果用户已通过验证,它将从返回值中提取唯一的玩家 ID。返回值看起来像http://steamcommunity.com/openid/id/76561197960435530
,你只需要76561197960435530
part.
此时您可以查询Steam来获取玩家信息。在提供的示例中,查询用户并显示基本玩家信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)