Android Open Source - Antares O Auth Activity






From Project

Back to project page Antares.

License

The source code is released under:

Apache License

If you think the Android project Antares 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.takac_j30.antares;
/*from   w w w.  ja  v a  2 s . c  om*/
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class OAuthActivity extends Activity {
  private String callbackUrl;
  private Twitter tw;
  private RequestToken req = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.oauth);

    callbackUrl = "callback://callbackactivity";
        tw = TwitterUtils.getTwitterInstance(this);

    findViewById(R.id.ExeOAuth).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo nInfo = cm.getActiveNetworkInfo();
        if(nInfo != null) {
          startAuthorize();
          findViewById(R.id.ExeOAuth).setVisibility(View.GONE);
        } else {
          Toast.makeText(getApplicationContext(), "?????????????????????????????\n???, ??????????????????????", Toast.LENGTH_LONG).show();
        }

      }
    });
  }

  private void startAuthorize() {
        AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                try {
                    req = tw.getOAuthRequestToken(callbackUrl);
                    return req.getAuthorizationURL();
                } catch (TwitterException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String url) {
                if (url != null) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                } else {
                    // ??
                }
            }
        };
        task.execute();
    }

    @Override
    public void onNewIntent(Intent intent) {
        if (intent == null
                || intent.getData() == null
                || !intent.getData().toString().startsWith(callbackUrl)) {
            return;
        }
        String verifier = intent.getData().getQueryParameter("oauth_verifier");

        AsyncTask<String, Void, AccessToken> task = new AsyncTask<String, Void, AccessToken>() {
            @Override
            protected AccessToken doInBackground(String... params) {
                try {
                    return tw.getOAuthAccessToken(req, params[0]);
                } catch (TwitterException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(AccessToken accessToken) {
                if (accessToken != null) {
                    // ??????
                    successOAuth(accessToken);
                } else {
                    // ?????
                    showToast("?????");
                    finish();
                }
            }
        };
        task.execute(verifier);
    }

    private void successOAuth(AccessToken accessToken) {
        TwitterUtils.storeAccessToken(this, accessToken);
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
}




Java Source Code List

com.takac_j30.antares.Account.java
com.takac_j30.antares.AppInformation.java
com.takac_j30.antares.AutoReply.java
com.takac_j30.antares.AutoTweetAdapter.java
com.takac_j30.antares.CustomPagerAdapter.java
com.takac_j30.antares.MainActivity.java
com.takac_j30.antares.OAuthActivity.java
com.takac_j30.antares.ReplySettings.java
com.takac_j30.antares.TweetSettings.java
com.takac_j30.antares.TwitterUtils.java