steam login using openID in android

跟風遠走 提交于 2020-05-09 20:33:07

问题


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?

回答1:


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

            }
        }



回答2:


I corrected code of @LibBo. There was a few syntax errors. Also updated ActionBarActivity to 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

                }
            }
        });
    }
}



回答3:


Use pure HTML in a form tag

This way you can cover Android devices AND everything that speaks HTML. This achieves the login as outlined in the official 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>
  • The user will click the button in this form on your site and will be redireced to the Steam community login page.
  • The user can then log into their Steam account on the Steam community pages.
  • With YourDomainUrlToReturnTo you specify where the user retuns to your site after successful login via Steam.
  • Steam will provide the Steam ID in the browser's location object.
  • With that Steam ID you can fetch user info via the Steam Web API.


来源:https://stackoverflow.com/questions/29873941/steam-login-using-openid-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!