Skip to content

Instantly share code, notes, and snippets.

@rhenium
Created December 7, 2013 04:18
Show Gist options
  • Save rhenium/7837212 to your computer and use it in GitHub Desktop.
Save rhenium/7837212 to your computer and use it in GitHub Desktop.
Twitter for Android を使って OAuth 認証できるっぽいので
package jp.rhe.test.twitterauth;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private final int REQUEST_CODE = 280414022;
private static final ComponentName AUTH_ACTIVITY = new ComponentName("com.twitter.android", "com.twitter.android.AuthorizeAppActivity");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent localIntent = new Intent().setComponent(AUTH_ACTIVITY);
localIntent.putExtra("ck", "IQKbtAYlXLripLGPWd0HUA");
localIntent.putExtra("cs", "GgDYlkSvaPxGxC4X8liwpUoqKwwr3lCADbz8A7ADU");
startActivityForResult(localIntent, REQUEST_CODE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && data != null) {
// requestCode == REQUEST_CODE && data == null は Cancel?
Bundle extras = data.getExtras();
((TextView) findViewById(R.id.textView1)).setText("Access Token: " + extras.getString("tk"));
((TextView) findViewById(R.id.textView2)).setText("Access Token Secret: " + extras.getString("ts"));
((TextView) findViewById(R.id.textView3)).setText("Screen Name: " + extras.getString("screen_name"));
((TextView) findViewById(R.id.textView4)).setText("User ID: " + extras.getLong("user_id"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment