首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Steam身份验证

Steam身份验证
EN

Stack Overflow用户
提问于 2013-09-07 21:56:27
回答 1查看 60.7K关注 0票数 24

在开始之前,我想说我对OpenID一无所知。我甚至不想做OpenID被用来做的事情,但我想人们会提到它,但那不是我想要的。

我有软件。该软件要求用户在注册时提供他们的Steam用户名。他们不是通过Steam登录,只是提供他们的用户名,以便其他人知道他们的steam用户名。因此,没有必要使用OpenID。

我知道,我可以简单地添加一个文本字段,让他们列出他们的Steam用户名,然后就可以结束了。然而,这样做,人们可以输入几乎任何他们想要和完成的steam用户名。相反,我希望能够确认他们的用户名。

理想情况下,应该有一个“验证steam帐户”按钮。人们点击它,它就会打开一个Steam登录表单。人们登录,然后steam返回他们的用户名(也许还有一些额外的数据,比如他们的头像)。做这件事最好的方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-08 12:39:56

有必要使用OpenID。这是Valve根据他们的documentation使用的方法。

你没有提到你的应用程序是用什么编写的,所以我只能猜测你是通过网页来做这件事的。在这种情况下,我建议使用LightOpenID库。在此基础上,此示例代码应该能够帮助您入门。

代码语言:javascript
运行
复制
<?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 Login ID按钮。当它被点击时,它会将用户重定向到Steam Community登录页面。登录后,用户被重定向回您在LightOpenID构造函数上设置的页面。如果用户已经通过验证,则从返回值中拉取唯一的播放器ID。返回值看起来像http://steamcommunity.com/openid/id/76561197960435530,您只需要76561197960435530部分。

此时,您可以查询Steam来获取球员信息。在提供的示例中,查询用户并显示播放器的基本信息。

票数 41
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18674042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档