Steam API身份验证 [英] Steam API Authentication

查看:237
本文介绍了Steam API身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开始之前,先说一点我对OpenID完全一无所知.我什至不想做OpenID的用途,但是我想人们会提到它,但这不是我想要的.

Before I get started, let me say I know absolutely nothing about OpenID. I don't even want to do what OpenID is used for, but I imagine people will mention it, but thats not what I'm looking for.

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

I have software. That software requires users to provide their Steam Username when they register. They are not signing on through Steam, just providing their username so that others know their steam username. So there is no need for OpenID.

我知道,我只需要添加一个文本字段,然后让他们列出自己的Steam用户名并将其命名为"day"即可.然而,这样做,人们几乎可以输入他们想要完成的任何Steam用户名.相反,我希望能够确认其用户名.

I know, I can simply just add a text field and have them list their Steam username and call it a day. However, doing this, people can input pretty much any steam username they want and be done. I would instead like to be able to confirm their usernames.

理想情况下,会有一个验证Steam帐户"按钮.人们单击它,它会弹出一个Steam登录表单.人们登录,然后Steam返回他们的用户名(也许还有一些额外的数据,例如他们的化身).最好的方法是什么?

Ideally, there would be an "authenticate steam account" button. People click it, and it brings up a Steam login form. People login, and then steam returns their username (and maybe some extra data, such as their avatar). What would be the best way to do this?

推荐答案

需要OpenID.这就是Valve根据其文档使用的方法.

There is a need for OpenID. That's the method that Valve uses according to their documentation.

您没有提到应用程序是用什么编写的,因此我只能猜测您是通过网页执行的.在这种情况下,我建议使用 LightOpenID 库.从这里开始,此示例代码应该可以帮助您入门.

You don't mention what your application is written in, so I can only guess that you are doing this via a web page. In that case, I recommend using the LightOpenID library. From there, this sample code should be able to get you started.

<?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部分.

Using this, it will present the user with a Steam Login ID button. When it is clicked it will redirect the user to the Steam Community login page. After they login, the user is redirect back to your page, that you set on the LightOpenID constructor. If the user has been validated, it will pull the unique player ID from the returned value. That returned value looks like http://steamcommunity.com/openid/id/76561197960435530, and you need just the 76561197960435530 part.

这时,您可以查询Steam以获取玩家信息.在提供的样本中,查询用户并显示基本玩家信息.

At this point you can query Steam to get player information. In the sample provided, the user is queried and basic player information is displayed.

这篇关于Steam API身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆