Android Open Source - dw2020 O Auth Authorization






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.com*/
import android.util.Log;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.fivehundredpx.api.FiveHundredException;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class OAuthAuthorization {

  private String consumerKey;
  private String consumerSecret;

  private String requestOauthToken;
  private String requestOauthSecret;

  private String url = "https://api.500px.com/v1";
  private String request_token_url = url+"/oauth/request_token";
  private String access_token_url = url+"/oauth/access_token";
  private String authorize_url = url+"/oauth/authorize";

  
  public static OAuthAuthorization build(String url, String k, String s) throws FiveHundredException {
    Builder builder = new Builder();

    OAuthAuthorization oauth = builder.url(url).consumerKey(k).consumerSecret(s)
        .build();
    
    return oauth;

  }

  private OAuthAuthorization() {

  }

  private void init() throws Exception {
    CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(
        this.consumerKey, this.consumerSecret);
    
    CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(
        request_token_url, access_token_url, authorize_url);

    final String urlString = provider.retrieveRequestToken(consumer, "");
    
    this.requestOauthToken = HttpParameterUtil.getUrlParamValue(urlString,
        "oauth_token");
    this.requestOauthSecret = consumer.getTokenSecret();

  }

    public boolean authorize(OAuthProvider provider)
            throws FiveHundredException {
        try {
            final HttpPost post = new HttpPost(authorize_url);

            provider.setOAuthConsumer(consumerKey, consumerSecret);
            provider.setOAuthRequestToken(requestOauthToken, requestOauthSecret);

            provider.signForAccessToken(post);

            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(post);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                String responseString = EntityUtils.toString(entity, "UTF-8");

                //throw new FiveHundredException(responseString);
                return false;
            }

            return true;
        } catch (FiveHundredException e) {
            throw e;
        } catch (Exception e) {
            throw new FiveHundredException(e);
        }
    }

    public AccessToken getAccessToken(OAuthProvider provider)
            throws FiveHundredException {
        try {
            final HttpPost post = new HttpPost(access_token_url);

            provider.setOAuthConsumer(consumerKey, consumerSecret);
            provider.setOAuthRequestToken(requestOauthToken, requestOauthSecret);

            provider.signForAccessToken(post);

            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(post);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                throw new FiveHundredException(statusCode);
            }

            return new AccessToken(response);

        } catch (ClientProtocolException e) {
            Log.e("OAuthAuthorization", "Failed", e);
            throw new FiveHundredException(e);
        } catch (IOException e) {
            Log.e("OAuthAuthorization", "Failed", e);
            throw new FiveHundredException(e);
        }
    }


  @Override
  protected Object clone() throws CloneNotSupportedException {

    final OAuthAuthorization c = new OAuthAuthorization();
    c.consumerKey = this.consumerKey;
    c.consumerSecret = this.consumerSecret;

    return c;
  }

  public static class Builder {
    private OAuthAuthorization instance;

    public Builder() {
      this.instance = new OAuthAuthorization();
    }

    public Builder url(String url){
      this.instance.url = url;
      return this;
    }
    
    public Builder consumerKey(String consumerKey) {
      this.instance.consumerKey = consumerKey;
      return this;
    }

    public Builder consumerSecret(String s) {
      this.instance.consumerSecret = s;
      return this;
    }

    public OAuthAuthorization build() throws FiveHundredException {
      try {
        OAuthAuthorization i = (OAuthAuthorization) instance.clone();
        i.init();
        return i;
      } catch (CloneNotSupportedException e) {
        return null;
      } catch (Exception e) {
        throw new FiveHundredException(e);
      }
    }
  }
}




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