Example usage for twitter4j HttpParameter encode

List of usage examples for twitter4j HttpParameter encode

Introduction

In this page you can find the example usage for twitter4j HttpParameter encode.

Prototype

public static String encode(String value) 

Source Link

Usage

From source file:com.daiv.android.twitter.utils.api_helper.APIHelper.java

License:Apache License

/**
 * Gets the header to verify the user on Twitter
 * @param twitter Coming from Twitter.getInstance()
 * @return String of the header to be used with X-Verify-Credentials-Authorization
 *//*from  ww w. ja v  a  2 s . c  o m*/
public String getAuthrityHeader(Twitter twitter) {
    try {
        // gets the system time for the header
        long time = System.currentTimeMillis() / 1000;
        long millis = time + 12;

        // set the necessary parameters
        List<HttpParameter> oauthHeaderParams = new ArrayList<HttpParameter>(5);
        oauthHeaderParams.add(new HttpParameter("oauth_consumer_key", AppSettings.TWITTER_CONSUMER_KEY));
        oauthHeaderParams.add(new HttpParameter("oauth_signature_method", "HMAC-SHA1"));
        oauthHeaderParams.add(new HttpParameter("oauth_timestamp", time + ""));
        oauthHeaderParams.add(new HttpParameter("oauth_nonce", millis + ""));
        oauthHeaderParams.add(new HttpParameter("oauth_version", "1.0"));
        oauthHeaderParams.add(new HttpParameter("oauth_token", twitter.getOAuthAccessToken().getToken()));
        List<HttpParameter> signatureBaseParams = new ArrayList<HttpParameter>(oauthHeaderParams.size());
        signatureBaseParams.addAll(oauthHeaderParams);

        // create the signature
        StringBuilder base = new StringBuilder("GET").append("&")
                .append(HttpParameter.encode(constructRequestURL(SERVICE_PROVIDER))).append("&");
        base.append(HttpParameter.encode(normalizeRequestParameters(signatureBaseParams)));

        String oauthBaseString = base.toString();
        String signature = generateSignature(oauthBaseString, twitter.getOAuthAccessToken());

        oauthHeaderParams.add(new HttpParameter("oauth_signature", signature));

        // create the header to post
        return "OAuth " + encodeParameters(oauthHeaderParams, ",", true);
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.daiv.android.twitter.utils.api_helper.APIHelper.java

License:Apache License

/**
 * Generates the signature to use with the header
 * @param data base signature data//  w  ww.j av  a2 s .c o  m
 * @param token the user's access token
 * @return String of the signature to use in your header
 */
public String generateSignature(String data, AccessToken token) {
    byte[] byteHMAC = null;
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec spec;
        String oauthSignature = HttpParameter.encode(AppSettings.TWITTER_CONSUMER_SECRET) + "&"
                + HttpParameter.encode(token.getTokenSecret());
        spec = new SecretKeySpec(oauthSignature.getBytes(), "HmacSHA1");
        mac.init(spec);
        byteHMAC = mac.doFinal(data.getBytes());
    } catch (InvalidKeyException ike) {
        throw new AssertionError(ike);
    } catch (NoSuchAlgorithmException nsae) {
        throw new AssertionError(nsae);
    }
    return BASE64Encoder.encode(byteHMAC);
}

From source file:com.daiv.android.twitter.utils.api_helper.APIHelper.java

License:Apache License

/**
 * Encodes the parameters/*  w w  w . j  a  v  a 2  s.com*/
 * @param httpParams parameters you want to send
 * @param splitter character used to split the parameters
 * @param quot whether you should use quotations or not
 * @return string of the desired encoding
 */
public static String encodeParameters(List<HttpParameter> httpParams, String splitter, boolean quot) {
    StringBuilder buf = new StringBuilder();
    for (HttpParameter param : httpParams) {
        if (!param.isFile()) {
            if (buf.length() != 0) {
                if (quot) {
                    buf.append("\"");
                }
                buf.append(splitter);
            }
            buf.append(HttpParameter.encode(param.getName())).append("=");
            if (quot) {
                buf.append("\"");
            }
            buf.append(HttpParameter.encode(param.getValue()));
        }
    }
    if (buf.length() != 0) {
        if (quot) {
            buf.append("\"");
        }
    }
    return buf.toString();
}