Android Open Source - android-reddit Live Authenticator






From Project

Back to project page android-reddit.

License

The source code is released under:

MIT License

If you think the Android project android-reddit listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.pocketreddit.library.authentication;
//from www .j  a va 2  s  .  c  om
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.pocketreddit.library.net.HttpHelper;

public class LiveAuthenticator implements Authenticator {
    private static final String TAG = LiveAuthenticator.class.getName();

    private static final String LOGIN_URI = "https://ssl.reddit.com/api/login";
    private static final String PARAM_USERNAME = "user";
    private static final String PARAM_PASSWORD = "passwd";

    private static final String PARAM_API_TYPE = "api_type";
    private static final String API_TYPE_JSON = "json";

    public LoginResult authenticate(String username, String password)
            throws AuthenticationException {
        try {
            return new LoginResult(HttpHelper.getInstance().getJsonObjectFromPost(
                    new URI(LOGIN_URI + "/" + username),
                    nameValuePairs(PARAM_USERNAME, username, PARAM_PASSWORD, password,
                            PARAM_API_TYPE, API_TYPE_JSON)));
        } catch (Exception e) {
            Log.e(TAG, "Unable to authenticate user: " + username, e);
            throw new AuthenticationException("Unable to authenticate user: " + username, e);
        }
    }

    private List<NameValuePair> nameValuePairs(String... keysAndValues) {
        if (keysAndValues.length % 2 != 0) {
            throw new IllegalArgumentException("Must contain keys and values, length cannot be odd");
        }

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();

        for (int i = 0; i < keysAndValues.length; i += 2) {
            pairs.add(new BasicNameValuePair(keysAndValues[i], keysAndValues[i + 1]));
        }

        return pairs;
    }
}




Java Source Code List

com.pocketreddit.library.Constants.java
com.pocketreddit.library.Created.java
com.pocketreddit.library.JsonParsingException.java
com.pocketreddit.library.Votable.java
com.pocketreddit.library.authentication.AuthenticationException.java
com.pocketreddit.library.authentication.Authenticator.java
com.pocketreddit.library.authentication.LiveAuthenticator.java
com.pocketreddit.library.authentication.LoginResult.java
com.pocketreddit.library.datasources.DataSourceException.java
com.pocketreddit.library.datasources.JsonDataSource.java
com.pocketreddit.library.datasources.LiveDataSource.java
com.pocketreddit.library.datasources.RedditDataSource.java
com.pocketreddit.library.net.HttpHelper.java
com.pocketreddit.library.net.NetException.java
com.pocketreddit.library.things.Account.java
com.pocketreddit.library.things.Comment.java
com.pocketreddit.library.things.Kind.java
com.pocketreddit.library.things.Link.java
com.pocketreddit.library.things.Listing.java
com.pocketreddit.library.things.Message.java
com.pocketreddit.library.things.More.java
com.pocketreddit.library.things.Subreddit.java
com.pocketreddit.library.things.Thing.java
com.pocketreddit.library.things.UserSubmittedContent.java
com.pocketreddit.library.things.factories.CommentFactory.java
com.pocketreddit.library.things.factories.LinkFactory.java
com.pocketreddit.library.things.factories.ListingFactory.java
com.pocketreddit.library.things.factories.SubredditFactory.java
com.pocketreddit.library.things.factories.ThingFactoryException.java
com.pocketreddit.library.things.factories.ThingFactory.java
com.pocketreddit.library.things.utils.JsonToThingConverter.java
com.pocketreddit.library.utils.StreamUtils.java
com.pocketreddit.library.utils.UtilsException.java