2015년 1월 26일 월요일

[Android][Java] 트위터(Twitter) 연동 로그인(login)

1. 트위터를 통한 로그인 기능

   1) 프로젝트 생성

   2) AndroidManifest.xml 에서 activity 추가

        <activity

            android:name="com.example.testtwitter.SendTwitter"

            android:label="@string/app_name"

            android:screenOrientation="portrait" >

            <intent-filter>

                <action android:name="android.intent.action.VIEW"/> 

                <category android:name="android.intent.category.DEFAULT"/> 

                 <category android:name="android.intent.category.BROWSABLE"/> 

                 <data android:scheme="testtwitter" android:host="testtwitter.sendtwitter" /> 

          </intent-filter>

        </activity> 



   3) MainActivity.java

public class MainActivity extends Activity {

 

 private TwitterLoginButton loginButton;

 

 // Note: Your consumer key and secret should be obfuscated in your source code before shipping.

 private static final String TWITTER_KEY = "트위터 개발자 홈페이지에서 발급";

 private static final String TWITTER_SECRET = "https://dev.twitter.com/";

 



 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);

  Fabric.with(this, new Twitter(authConfig));

  setContentView(R.layout.activity_main);

  

  loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);

        loginButton.setCallback(new Callback() {

            @Override

            public void success(Result result) {

                // Do something with result, which provides a TwitterSession for making API calls

             TwitterSession session = Twitter.getSessionManager().getActiveSession();

             TwitterAuthToken authToken = session.getAuthToken();

             String token = authToken.token;

             String secret = authToken.secret;

             Log.d("token", token);

             Log.d("secret", secret);

            }



            @Override

            public void failure(TwitterException exception) {

                // Do something on failure

            }

        });

 }

 

 @Override

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

        super.onActivityResult(requestCode, resultCode, data);

        loginButton.onActivityResult(requestCode, resultCode, data);

    }

}
4) main_activity.xml

    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <com.twitter.sdk.android.core.identity.TwitterLoginButton
        android:id="@+id/twitter_login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
</LinearLayout>

댓글 없음:

댓글 쓰기