Android Open Source - cheepcheep O Auth Activity






From Project

Back to project page cheepcheep.

License

The source code is released under:

Apache License

If you think the Android project cheepcheep 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 org.tarrio.cheepcheep;
/*from   w  ww  .j  a va  2  s  . com*/
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthException;

import org.tarrio.cheepcheep.http.HttpClientFactory;
import org.tarrio.cheepcheep.http.OAuthHttpClient;
import org.tarrio.cheepcheep.model.Preferences;
import org.tarrio.cheepcheep.service.PreferencesProvider;
import org.tarrio.cheepcheep.task.AsyncTwitterTask;
import org.tarrio.cheepcheep.task.TaskCallback;
import org.tarrio.cheepcheep.task.VerifyCredentialsTask;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class OAuthActivity extends Activity {

  public static final String CALLBACK_URL = "cheepcheep://auth";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final PreferencesProvider preferencesProvider = new PreferencesProvider(
        this);

    Intent i = getIntent();
    if (i.getAction().equals(Constants.ACTION_OAUTH)) {
      signIntoTwitterWebpage(preferencesProvider);
    } else {
      Uri uri = i.getData();
      verifyCredentialsAndReturnToHomeTimeline(preferencesProvider, uri);
    }
  }

  private void verifyCredentialsAndReturnToHomeTimeline(
      final PreferencesProvider preferencesProvider, Uri uri) {
    final Preferences pref = preferencesProvider.get();
    extractCredentials(pref, uri);
    preferencesProvider.save(pref);

    if (!"".equals(pref.getUsername()))
      returnToHomeTimeline();

    TaskCallback callback = new TaskCallback() {
      @Override
      public void onSuccess(AsyncTwitterTask task) {
        pref.setUsername(((VerifyCredentialsTask) task).getUser()
            .getScreenName());
        preferencesProvider.save(pref);

        returnToHomeTimeline();
      }

      @Override
      public void onFailure(int statusCode, AsyncTwitterTask task) {
        returnToHomeTimeline();
      }
    };
    new VerifyCredentialsTask(this, pref, callback).run();
  }

  private void returnToHomeTimeline() {
    Intent mainWindow = new Intent(this, HomeTimelineActivity.class);
    mainWindow.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(mainWindow);
  }

  public void signIntoTwitterWebpage(PreferencesProvider preferencesProvider) {
    OAuthHttpClient client = (OAuthHttpClient) HttpClientFactory
        .getClient(preferencesProvider.get());
    String authUrl;
    try {
      authUrl = client.getProvider().retrieveRequestToken(CALLBACK_URL);
    } catch (OAuthException e) {
      throw new RuntimeException(
          "Error getting the request URL for OAuth.", e);
    } catch (OAuthCommunicationException e) {
      throw new RuntimeException(
          "Error getting the request URL for OAuth.", e);
    }
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
    i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    startActivity(i);
  }

  private void extractCredentials(Preferences pref, Uri uri) {
    String verifier = uri.getQueryParameter("oauth_verifier");
    OAuthHttpClient client = (OAuthHttpClient) HttpClientFactory
        .getClient(pref);
    try {
      client.getProvider().retrieveAccessToken(verifier);
      pref.setConsumerToken(client.getConsumer().getToken());
      pref.setConsumerSecret(client.getConsumer().getTokenSecret());
    } catch (OAuthException e) {
      throw new RuntimeException(e);
    } catch (OAuthCommunicationException e) {
      throw new RuntimeException(e);
    }
  }

}




Java Source Code List

org.tarrio.cheepcheep.Constants.java
org.tarrio.cheepcheep.HomeTimelineActivity.java
org.tarrio.cheepcheep.OAuthActivity.java
org.tarrio.cheepcheep.SingleTweetActivity.java
org.tarrio.cheepcheep.TweetListActions.java
org.tarrio.cheepcheep.TweetListAdapter.java
org.tarrio.cheepcheep.UserInfoActivity.java
org.tarrio.cheepcheep.dialog.CheepCheepDialog.java
org.tarrio.cheepcheep.dialog.ConfirmDeleteTweetDialog.java
org.tarrio.cheepcheep.dialog.NewTweetDialog.java
org.tarrio.cheepcheep.exceptions.AuthError.java
org.tarrio.cheepcheep.exceptions.CheepCheepException.java
org.tarrio.cheepcheep.exceptions.NetError.java
org.tarrio.cheepcheep.exceptions.ParseError.java
org.tarrio.cheepcheep.exceptions.TweetNotFoundError.java
org.tarrio.cheepcheep.exceptions.UserNotFoundError.java
org.tarrio.cheepcheep.http.HttpClientFactory.java
org.tarrio.cheepcheep.http.OAuthHttpClient.java
org.tarrio.cheepcheep.model.Preferences.java
org.tarrio.cheepcheep.model.Tweet.java
org.tarrio.cheepcheep.model.User.java
org.tarrio.cheepcheep.service.PreferencesProvider.java
org.tarrio.cheepcheep.service.TwitterJSONSerializer.java
org.tarrio.cheepcheep.service.TwitterService.java
org.tarrio.cheepcheep.service.TwitterStatusSaverService.java
org.tarrio.cheepcheep.task.AsyncTwitterTask.java
org.tarrio.cheepcheep.task.CreateNewTweetTask.java
org.tarrio.cheepcheep.task.DeleteTweetTask.java
org.tarrio.cheepcheep.task.FollowUnfollowUserTask.java
org.tarrio.cheepcheep.task.GetSingleTweetTask.java
org.tarrio.cheepcheep.task.GetUserInfoTask.java
org.tarrio.cheepcheep.task.TaskCallback.java
org.tarrio.cheepcheep.task.UpdateTweetsTask.java
org.tarrio.cheepcheep.task.VerifyCredentialsTask.java