在Android中使用openID进行Steam登录 [英] steam login using openID in android

查看:462
本文介绍了在Android中使用openID进行Steam登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手.我的项目是使用Steam公共API制作应用程序,但我不知道如何允许用户使用Steam帐户登录.

Steam的Web API文档指出我应该使用openID,因此我进行了大量搜索以查找在andorid应用中实现openID的示例,但是解决方案

我认为我发现了某种解决方法.

steam openid可以与以下网址请求一起使用:

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是将显示在登录屏幕上的网站,验证完成后,用户也将重定向到该网站,它实际上不必存在. 之后,您要做的就是解析新URL作为用户ID.

所以我用了这样的东西

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);

            }
        }

Iam new to android development. my project is to make an application using steam public API but i couldn't figure out how to allow the user to login using steam account.

Steam's web API documentation states that i should use openID, So i searched alot to find an example implementing openID in an andorid app, but this is the only example i found and it doesn't work, the webView just turns out blank.

i just want the user to click on a login button which fires a webView where he user can login and then get his steam ID back.

so my question is

  1. Is there a way to implement openID login in android?
  2. If not, is there anyway to allow the user to login steam?

解决方案

I think i discovered some sort of a workaround i guess.

The steam openid can be used with a url request like this:

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/

where REALM_PARAM is the website that will appear on the login screen, Also the user will be redirected to that website after authentication is complete, it doesn't have to actually exist. All you had to do after that is parse the new url for the user id.

So i used something like this

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);

            }
        }

这篇关于在Android中使用openID进行Steam登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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