๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ปTech/๐ŸŒตandroid

[์•ˆ๋“œ๋กœ์ด๋“œ] ํŽ˜์ด์Šค๋ถ ๋กœ๊ทธ์ธ ๊ฐ„๋‹จ ์—ฐ๋™

by _viper_ 2015. 11. 18.
๋ฐ˜์‘ํ˜•

ํŽ˜์ด์Šค๋ถ 4.x ๋ฒ„์ „ SDK ์ ์šฉํ•˜๊ณ  ํŽ˜์ด์Šค๋ถ ๋กœ๊ทธ์ธ ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํฌ์ŠคํŒ… ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด์ „ ๋ฒ„์ „ SDK ์ ์šฉ์‹œ ํŽ˜์ด์Šค๋ถ ์•ฑ์ด ์„ค์น˜๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ œ๋Œ€๋กœ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ•˜๋Š” ์˜ค๋ฅ˜๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ 

์ €๋Š” ์ตœ์‹  ๋ฒ„์ „ SDK ์ ์šฉ ํ›„ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋„ค์š”.

 

๊ธฐ๋ณธ์ ์œผ๋กœ developers.facebook์— ํŽ˜์ด์Šค๋ถ๊ณผ ์—ฐ๋™ํ•  ์•ฑ ๋“ฑ๋กํ•˜์‹œ๊ณ  App ID๋Š” ๊ฐ€์ง€๊ณ  ์žˆ์œผ์…”์•ผํ•ฉ๋‹ˆ๋‹ค.

AndroidManifest.xml์— App ID๋ฅผ ๋“ฑ๋ก ํ•ด์ค˜์•ผ ํ•˜๊ฑฐ๋“ ์š”.

ํŽ˜์ด์Šค๋ถ์— ์•ฑ ๋“ฑ๋กํ•˜๋Š” ๋ถ€๋ถ„์€ ๊ฒ€์ƒ‰ํ•˜๋ฉด ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค

 

1. ์•„๋ž˜ ๋งํฌ์—์„œ 4 ๋ฒ„์ „์— SDK ๋‹ค์šด ๋ฐ›์•„์ฃผ์„ธ์š”

https://developers.facebook.com/docs/android/downloads

  

2. SDK ์••์ถ• ํ‘ธ์‹œ๊ณ  ํ”„๋กœ์ ํŠธ Import ํ›„ bolts ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”

 

3. AndroidManifest.xml ์ž‘์„ฑ

       <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="app_id" />

        <activity
            android:name="com.facebook.FacebookActivity"/>

App ID ์™€ FacebookActivity ๋“ฑ๋กํ•ด์ฃผ์„ธ์š”. App ID๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•œ ์•„์ด๋”” ์ ์œผ์„ธ์š”

FacebooActivity ์ œ๊ฐ€ ์ž‘์—…ํ•˜๋Š” Activity๊ฐ€ ์•„๋‹Œ ์—ฐ๋™ ์‹œ ์ž๋™์œผ๋กœ ๋œจ๋Š” Activity์ž…๋‹ˆ๋‹ค.

  

4. Activity ์ž‘์„ฑ

import java.util.Arrays;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.corfire.stampy.utils.PreferenceUtil;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphRequest.GraphJSONObjectCallback;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;


public class FacebookTestActivity extends Activity {

    private CallbackManager callbackManager;

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

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

            LoginManager.getInstance().logInWithReadPermissions(FacebookTestActivity.this,
                                                                Arrays.asList("public_profile", "email"));
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

                @Override
                public void onSuccess(final LoginResult result) {

                    GraphRequest request;
                    request = GraphRequest.newMeRequest(result.getAccessToken(), new GraphJSONObjectCallback() {

                        @Override
                        public void onCompleted(JSONObject user, GraphResponse response) {
                            if (response.getError() != null) {

                            } else {
                                Log.i("TAG", "user: " + user.toString());
                                Log.i("TAG", "AccessToken: " + result.getAccessToken().getToken());
                                setResult(RESULT_OK);
                                finish();
                            }
                        }
                    });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email,gender,birthday");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onError(FacebookException error) {
                    Log.e("test", "Error: " + error);
                    finish();
                }

                @Override
                public void onCancel() {
                    finish();
                }
            });
        
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

 

์ด์ „ ๋ฒ„์ „์—์„œ๋Š” Session ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ ํŽ˜์ด์Šค๋ถ ๋กœ๊ทธ์ธ ์—ฐ๋™ ์ž‘์—…์ด ์ด๋ฃจ์–ด ์กŒ๋Š”๋ฐ

์ง€๊ธˆ์€ LoginManager ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋„ค์š”.

 

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๊ณ  ํ”„๋กœ์ ํŠธ ์‹คํ–‰ํ•ด ๋ณด๋ฉด Log์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด์™€ Token ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

์‹คํ–‰ํ•˜๋‹ค ๋ณด๋ฉด Key Hashes ๋ฌธ์ œ๋กœ ์—๋Ÿฌ๋กœ๊ทธ ๋‚˜ํƒ€๋‚˜๋Š”๋ฐ ๋กœ๊ทธ์— ์ฐํžŒ ํ•ด์‰ฌํ‚ค ๊ฐ’์„ 

ํŽ˜์ด์Šค๋ถ ๋””๋ฒจ๋กœํผ์— ๋“ฑ๋กํ•ด์ฃผ๋ฉด ์ •์ƒ์ ์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. 

 

์ถ”๊ฐ€๋กœ ํŽ˜์ด์Šค๋ถ ์—ฐ๋™ ๋กœ๊ทธ์•„์›ƒ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

LoginManager.getInstance().logOut();

 

*์œ„ ๋ฐฉ๋ฒ•์ด ์•ˆ๋œ๋‹ค๋ฉด ์•„๋ž˜ ๋‚ด์šฉ ์ฐธ์กฐํ•ด์ฃผ์„ธ์š”.

ํŽ˜์ด์Šค๋ถ SDK 4.5.1 ํ˜น์€ 4.6.0์ดํ›„๋กœ ๋ฐฉ๋ฒ•์ด ๋ฐ”๋€๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
(4.10.0 ๋ฒ„์ „๊ธฐ์ค€์œผ๋กœ ์ž‘์—…ํ•˜์˜€์œผ๋ฉฐ, 4.7.0 ๋ถ€ํ„ฐ ํ•ด๋‹น๋ฐฉ๋ฒ•์œผ๋กœ ์ง„ํ–‰ํ•ด์•ผ ๋˜๋Š”๊ฒƒ ๊ฐ™์Œ)
SDK ์••์ถ•ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ํ›„, ์••์ถ•์„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
facebook ํด๋”๊ฐ€ ์žˆ์„ํ…๋ฐ ํ•ด๋‹น ํด๋”์— .aar ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
1. facebook-android-sdk-4.x.x.arr ํŒŒ์ผ์„ facebook-android-sdk-4.x.x.zip ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
2. facebook-android-sdk-4.x.x.zip ํŒŒ์ผ์˜ ์••์ถ•์„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
3. ์••์ถ•์„ ํ•ด์ œํ•œ ํด๋”์— classes.jar ํŒŒ์ผ์„ ์›ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝ ํ›„ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
(ex. classes.jar -> facebook-android-sdk-4.x.x.jar)

์ด์ „์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ”„๋กœ์ ํŠธ ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋Œ€์‹ ์— jar ํŒŒ์ผ ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.