Android Open Source - dw2020 O Auth Parameters






From Project

Back to project page dw2020.

License

The source code is released under:

Apache License

If you think the Android project dw2020 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.fivehundredpx.api.auth;
// w  w w.  j  a v a 2s. c o m
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;

import android.net.Uri;

final public class OAuthParameters {
  /**
   * <p>
   * Parameters.
   * </p>
   */
  private HashMap<String, String> params;

  /**
   * <p>
   * Create an instance of OAuthParameters class.
   * </p>
   * @param consumerSecret Consumer secret.
   * @throws IllegalArgumentException If consumer key/secret is empty or null.
   */
  public OAuthParameters(String consumerKey) {
    if (consumerKey == null
        || (consumerKey = consumerKey.trim()).length() == 0) {
      throw new IllegalArgumentException(
        "Consumer key must not be empty/null");
    }
    //
    params = new HashMap<String,String>();
    params.put(OAuthConstants.TIMESTAMP, getTimestampInSeconds());
    params.put(OAuthConstants.SIGN_METHOD, "HMAC-SHA1");
    params.put(OAuthConstants.VERSION, "1.0");
    params.put(OAuthConstants.NONCE, getTimestampInSeconds());
    params.put(OAuthConstants.CONSUMER_KEY, consumerKey);
  }

  /**
   * <p>
   * Get a string with all the parameters sorted.
   * </p>
   * @return Sorted string.
   */
  public String getSortedEncodedParamsAsString() {
    StringBuffer buffer = new StringBuffer();
    String[] sKeys = sortedKeys();
    //
    for (int i = 0; i < sKeys.length; i++) {
      buffer.append(HttpParameterUtil.encode(sKeys[i]));
      buffer.append('=');
      buffer.append(HttpParameterUtil.encode((String)params.get(sKeys[i])));
      
      //
      if (i +1 < sKeys.length) {
        buffer.append('&');
      }
    }
    //
    return buffer.toString();
  }

  /**
   * 
   * OAuth key="value",
   * 
   * 
   * <p>
   * Get the Authorization header value.
   * </p>
   * @return Value.
   */
  public String getAuthorizationHeaderValue() {
    StringBuffer buffer = new StringBuffer();
    buffer.append("OAuth ");
    //
    String[] sKeys = sortedKeys();
    for (int i = 0; i < sKeys.length; i++) {
      if (sKeys[i].startsWith(OAuthConstants.PARAM_PREFIX)) {
        buffer.append(sKeys[i]);
        buffer.append('=');
        buffer.append('"');
        buffer.append(
            Uri.encode((String)params.get(sKeys[i])));
        buffer.append("\", ");
      }
    }
    //
    return buffer.toString().substring(0, buffer.length() - 2);
  }

  /**
   * <p>
   * Put a given parameter.
   * </p>
   * @param key Parameter key.
   * @param value Parameter value.
   */
  public void put(String key, String value) {
    params.put(key, value);
  }

  /**
   * <p>
   * Get an array with all parameter keys sorted.
   * </p>
   * @return Sorted keys.
   */
  private String[] sortedKeys() {
    final Set<String> keys = params.keySet();
    Object[] oKeys =  keys.toArray();
    Arrays.sort(oKeys);
    
    String[] sKeys = new String[oKeys.length];
    int i = 0;
    for (Object object : oKeys) {
      sKeys[i++] = object.toString();
    }
    
    return sKeys;
  }

  /**
   * <p>
   * Get the current time in seconds.
   * </p>
   * @return Timestamp.
   */
  private String getTimestampInSeconds() {
    return String.valueOf(System.currentTimeMillis() / 1000);
  }
}




Java Source Code List

com.fivehundredpx.api.FiveHundredException.java
com.fivehundredpx.api.PxApi.java
com.fivehundredpx.api.auth.AccessToken.java
com.fivehundredpx.api.auth.FacebookAuthProvider.java
com.fivehundredpx.api.auth.HttpParameterUtil.java
com.fivehundredpx.api.auth.OAuthAuthorization.java
com.fivehundredpx.api.auth.OAuthConstants.java
com.fivehundredpx.api.auth.OAuthParameters.java
com.fivehundredpx.api.auth.OAuthProvider.java
com.fivehundredpx.api.auth.PlusAuthProvider.java
com.fivehundredpx.api.auth.TwitterAuthProvider.java
com.fivehundredpx.api.auth.XAuthProvider.java
com.fivehundredpx.api.tasks.UserDetailTask.java
com.fivehundredpx.api.tasks.XAuth500pxTask.java
net.darkwire.example.BaseActivity.java
net.darkwire.example.BaseSpiceActivity.java
net.darkwire.example.MainActivity.java
net.darkwire.example.ObservableApp.java
net.darkwire.example.builder.FiveHundredPxAccessToken.java
net.darkwire.example.exception.AuthenticationError.java
net.darkwire.example.model.FiveHundredPxConfiguration.java
net.darkwire.example.model.FiveHundredPxImageMetadata.java
net.darkwire.example.model.FiveHundredPxPhotoContainer.java
net.darkwire.example.model.FiveHundredPxPhoto.java
net.darkwire.example.service.CacheableSpiceRequest.java
net.darkwire.example.service.FiveHundredPxGsonSpiceService.java
net.darkwire.example.service.FiveHundredPxJacksonSpiceService.java
net.darkwire.example.service.FiveHundredPxRecentPhotosSpiceRequest.java
net.darkwire.example.service.FiveHundredPxSearchSpiceRequest.java
net.darkwire.example.service.FiveHundredPxSpiceRequest.java
net.darkwire.example.service.FiveHundredPx.java
net.darkwire.example.service.catalog.UrlCatalog.java
net.darkwire.example.service.client.FiveHundredPxClient.java
net.darkwire.example.widgets.adapter.NumericPaginationBaseAdapter.java
net.darkwire.example.widgets.adapter.NumericPagination.java
net.darkwire.example.widgets.adapter.PhotoAdapter.java
net.darkwire.example.widgets.listener.InfiniteScrollListener.java
net.darkwire.example.widgets.view.SquareImageView.java
se.akerfeldt.signpost.retrofit.HttpRequestAdapter.java
se.akerfeldt.signpost.retrofit.RetrofitHttpOAuthConsumer.java
se.akerfeldt.signpost.retrofit.SigningOkClient.java