在 android 中使用 openID 登录 Steam

steam login using openID in android

我是 android 开发的新手。我的项目是使用 Steam public API 创建一个应用程序,但我不知道如何让用户使用 Steam 帐户登录。

Steam 的网络 API 文档指出我应该使用 openID,所以我搜索了很多以找到在 andorid 应用程序中实现 openID 的示例,但 this 是我找到的唯一示例,但它没有'工作,webView 只是空白。

我只想让用户点击一个登录按钮,该按钮会触发一个 webView,用户可以在其中登录,然后取回他的 Steam ID。

所以我的问题是

  1. 有没有办法在android中实现openID登录?
  2. 如果没有,是否允许用户登录 steam?

我想我发现了某种解决方法。

Steam openid 可以与这样的 url 请求一起使用:

https://steamcommunity.com/openid/login?
openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&
openid.identity=http://specs.openid.net/auth/2.0/identifier_select&
openid.mode=checkid_setup&
openid.ns=http://specs.openid.net/auth/2.0&
openid.realm=https://REALM_PARAM&
openid.return_to=https://REALM_PARAM/signin/

其中 REALM_PARAM 是将出现在登录屏幕上的网站,并且用户将在身份验证完成后被重定向到该网站,该网站不一定实际存在。 之后您所要做的就是解析用户 ID 的新 url。

所以我用了这样的东西

public class LoginActivity extends ActionBarActivity {

    // The string will appear to the user in the login screen  
    // you can put your app's name
    final String REALM_PARAM = "YourAppName";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final WebView webView = new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url,
                                      Bitmap favicon) {

                //checks the url being loaded
                setTitle(url);
                Uri Url = Uri.parse(url);

                if(Url.getAuthority().equals(REALM_PARAM.toLowerCase())){
                    // That means that authentication is finished and the url contains user's id.
                    webView.stopLoading();

                    // Extracts user id.
                    Uri userAccountUrl = Uri.parse(Url.getQueryParameter("openid.identity"));
                    String userId = userAccountUrl.getLastPathSegment();

                    // Do whatever you want with the user's steam id 

                });
                setContentView(webView);

                // Constructing openid url request
                String url = "https://steamcommunity.com/openid/login?" +
                        "openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&" +
                        "openid.identity=http://specs.openid.net/auth/2.0/identifier_select&" +
                        "openid.mode=checkid_setup&" +
                        "openid.ns=http://specs.openid.net/auth/2.0&" +
                        "openid.realm=https://" + REALM_PARAM + "&" +
                        "openid.return_to=https://" + REALM_PARAM + "/signin/";

                webView.loadUrl(url);

            }
        }

我更正了@LibBo 的代码。有一些语法错误。还将 ActionBarActivity 更新为 AppCompatActivity

public class SteamActivity extends AppCompatActivity {

    // The string will appear to the user in the login screen
    // you can put your app's name
    final String REALM_PARAM = "YourAppName";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_steam);


        final WebView webView = new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;

        setContentView(webView);

        // Constructing openid url request
        String url = "https://steamcommunity.com/openid/login?" +
                "openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&" +
                "openid.identity=http://specs.openid.net/auth/2.0/identifier_select&" +
                "openid.mode=checkid_setup&" +
                "openid.ns=http://specs.openid.net/auth/2.0&" +
                "openid.realm=https://" + REALM_PARAM + "&" +
                "openid.return_to=https://" + REALM_PARAM + "/signin/";

        webView.loadUrl(url);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url,
                                      Bitmap favicon) {

                //checks the url being loaded
                setTitle(url);
                Uri Url = Uri.parse(url);

                if (Url.getAuthority().equals(REALM_PARAM.toLowerCase())) {
                    // That means that authentication is finished and the url contains user's id.
                    webView.stopLoading();

                    // Extracts user id.
                    Uri userAccountUrl = Uri.parse(Url.getQueryParameter("openid.identity"));
                    String userId = userAccountUrl.getLastPathSegment();

                    // Do whatever you want with the user's steam id

                }
            }
        });
    }
}

形式标签中使用纯HTML

这样您就可以覆盖 Android 设备和所有会说话的东西 HTML。这样就实现了官方描述的登录Steam documentation.

    <form action="https://steamcommunity.com/openid/login" method="post">
        <input type="hidden" name="openid.identity"
               value="http://specs.openid.net/auth/2.0/identifier_select" />
        <input type="hidden" name="openid.claimed_id"
               value="http://specs.openid.net/auth/2.0/identifier_select" />
        <input type="hidden" name="openid.ns" value="http://specs.openid.net/auth/2.0" />
        <input type="hidden" name="openid.mode" value="checkid_setup" />
        <input type="hidden" name="openid.realm" value="https:\yourOpenIdRealm.com" />
        <input type="hidden" name="openid.return_to" value="https:\YourDomainUrlToReturnTo.com" />
        <Button type="submit">Log in through Steam</Button>
    </form>
  • 用户将在您的站点上单击此表单中的按钮,然后将被重定向到 Steam 社区登录页面。
  • 用户随后可以在 Steam 社区页面上登录他们的 Steam 帐户。
  • 使用 YourDomainUrlToReturnTo 您可以指定用户通过 Steam 成功登录后返回到您网站的位置。
  • Steam 将在浏览器 location object 中提供 Steam ID。
  • 使用该 Steam ID,您可以通过 Steam Web API.
  • 获取用户信息