33 ソーシャル

Javaでソーシャル開発の手段をまとめる

Twitter

アプリの登録 https://dev.twitter.com/apps

Twitter4J http://twitter4j.org/ja/index.html

Twitterとアクセスする流れ

1.アプリ側がTwitterにCONSUME_KEYとCONSUME_SECRETを渡す

2.ユーザ側がTwitterにログインし、PINコードを取得

3.アプリ側がPINコードでTwitterから取得する情報を処理

サンプル

import twitter4j.Twitter;

import twitter4j.TwitterException;

import twitter4j.TwitterFactory;

import twitter4j.auth.AccessToken;

import twitter4j.auth.RequestToken;

import android.app.ListActivity;

import android.app.ProgressDialog;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.widget.Toast;

public class MainActivity extends ListActivity {

private static final String TAG = MainActivity.class.getSimpleName();

private static final int TWITTER_AUTH = 0;

private static final String CONSUME_KEY = "YourKey";

private static final String CONSUME_SECRET = "YourSecret";

private Twitter twitter = null;

private RequestToken requestToken = null;

private String authUrl = null;

private ProgressDialog dialog = null;

private Handler handler = new Handler();

// OKの場合

private Runnable normal = new Runnable() {

public void run() {

if(dialog != null){

dialog.dismiss();

}

// 認証処理を別Activityに任せ

Intent intent = new Intent(MainActivity.this, XXXActivity.class);

intent.putExtra("authurl", authUrl);

startActivityForResult(intent, TWITTER_AUTH);

}

};

// OKの場合

private Runnable update = new Runnable() {

public void run() {

// do something

if(dialog != null){

dialog.dismiss();

}

}

};

// NGの場合

private Runnable error = new Runnable() {

public void run() {

if(dialog != null){

dialog.dismiss();

}

Toast.makeText(MainActivity.this, R.string.auth_error, Toast.LENGTH_SHORT).show();

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.dialog = new ProgressDialog(this);

this.dialog.setMessage(getString(R.string.msg_wait));

this.dialog.setIndeterminate(true);

this.dialog.show();

new Thread() {

@Override

public void run() {

try {

// 認証を行う(一回目)

twitter = new TwitterFactory().getInstance();

twitter.setOAuthConsumer(CONSUME_KEY, CONSUME_SECRET);

requestToken = twitter.getOAuthRequestToken();

authUrl = requestToken.getAuthorizationURL();

handler.post(normal);

} catch (TwitterException e) {

Log.d(TAG, "Exception", e);

handler.post(error);

}

};

}.start();

};

@Override

protected void onStop() {

super.onStop();

if(this.twitter != null){

this.twitter.shutdown();

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

// 別Activityが結果を返す

if(requestCode == TWITTER_AUTH){

if(resultCode == 0){

final String pincode = data.getExtras().getString("pincode");

this.dialog = new ProgressDialog(this);

this.dialog.setMessage(getString(R.string.msg_wait));

this.dialog.setIndeterminate(true);

this.dialog.show();

new Thread() {

@Override

public void run() {

try {

AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, pincode);

twitter.shutdown();

twitter = null;

// 認証を行う(二回目)

twitter = new TwitterFactory().getInstance();

twitter.setOAuthConsumer(CONSUME_KEY, CONSUME_SECRET);

twitter.setOAuthAccessToken(accessToken);

} catch (TwitterException e) {

Log.d(TAG, "Exception", e);

handler.post(error);

}

handler.post(update);

};

}.start();

}

}

}

}

Facebook

アプリの登録 http://www.facebook.com/developers/

Facebook SDK https://github.com/facebook/facebook-android-sdk

Facebookとアクセスする流れ

1.アプリ側がFacebookにAPP_IDを渡す

2.ユーザ側がFacebookにログイン

3.アプリ側がFacebookから取得する情報を処理

サンプル

import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.MalformedURLException;

import com.facebook.android.AsyncFacebookRunner;

import com.facebook.android.AsyncFacebookRunner.RequestListener;

import com.facebook.android.DialogError;

import com.facebook.android.Facebook;

import com.facebook.android.Facebook.DialogListener;

import com.facebook.android.FacebookError;

import android.app.ListActivity;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends ListActivity {

private static final String TAG = MainActivity.class.getSimpleName();

private static final String APP_ID = "YourID";

private Facebook facebook = null;

private ProgressDialog dialog = null;

private Handler handler = new Handler();

// OKの場合

private Runnable normal = new Runnable() {

public void run() {

// do something

if(dialog != null){

dialog.dismiss();

}

}

};

// NGの場合

private Runnable error = new Runnable() {

public void run() {

if(dialog != null){

dialog.dismiss();

}

Toast.makeText(MainActivity.this, R.string.auth_error, Toast.LENGTH_SHORT).show();

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

if(this.facebook == null){

this.facebook = new Facebook(APP_ID);

}

// 権限の設定

String[] permissions = {"read_stream"};

if(!facebook.isSessionValid()){

this.facebook.authorize(this, permissions, new DialogListener() {

public void onComplete(Bundle values) {

dialog = new ProgressDialog(MainActivity.this);

dialog.setIndeterminate(true);

dialog.setMessage(getString(R.string.msg_wait));

dialog.show();

new AsyncFacebookRunner(facebook).request("me/home", new RequestListener() {

public void onComplete(String response, Object state) {

// JSONデータを解析

XXX xxx = XXXFactory.create(response);

if(xxx != null){

handler.post(normal);

}else{

handler.post(error);

}

}

public void onMalformedURLException(...) { }

public void onIOException(...) { }

public void onFileNotFoundException(...) { }

public void onFacebookError(...) { }

});

}

public void onFacebookError(FacebookError e) { }

public void onError(DialogError e) { }

public void onCancel() { }

});

}

};

@Override

protected void onStop() {

super.onStop();

if(facebook != null){

try {

facebook.logout(this);

} catch (MalformedURLException e) {

Log.d(TAG, "Exception", e);

} catch (IOException e) {

Log.d(TAG, "Exception", e);

}

}

}

}

Google

https://developers.google.com/console

https://developers.google.com/+/web/signin/