2016. 3. 29. 13:08ㆍAndroid
=> 페이스북 설정 참조
ㅇ hash key 가져오기(oncreate 함수 안에서)
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.test.facebooklogin", //-> 패키지명
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
ㅇ layout xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="180dip"
android:gravity="center"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="match_parent"
android:layout_height="40dip"
android:gravity="center"
android:text="Facebook 으로 회원가입/로그인이 가능합니다"
android:textColor="#000000"
android:textSize="14dip" >
</TextView>
<com.facebook.login.widget.LoginButton
android:id="@+id/connectWithFbButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:text=" connect_with_facebook" />
</LinearLayout>
ㅇ AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
- Application 태그 안에
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
ㅇ strings.xml
<string name="facebook_app_id">생성된 앱 ID</string>
ㅇ Activity
private CallbackManager callbackManager;
String id="";
String name="";
String email="";
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* You need to initialize Facebook SDK before you can use it.
* Add a call to FacebookSdk.sdkInitialize from onCreate in Activity or Application:
*/
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.connectWithFbButton);
loginButton.setReadPermissions("email");
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) { // 로그인이 성공되었을때 호출
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Application code
try {
id = (String) response.getJSONObject().get("id");//페이스북 아이디값
name = (String) response.getJSONObject().get("name");//페이스북 이름
email = (String) response.getJSONObject().get("email");//이메일
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// new joinTask().execute(); //자신의 서버에서 로그인 처리를 해줍니다
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "로그인을 취소 하였습니다!", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(MainActivity.this, "에러가 발생하였습니다", Toast.LENGTH_SHORT).show();
}
});
'Android' 카테고리의 다른 글
R.java (0) | 2016.04.03 |
---|---|
카카오톡 간편 로그인 (0) | 2016.03.29 |
Utils class (0) | 2016.03.28 |
include 태그 사용 (0) | 2016.03.28 |
EditText focus 없애기 (0) | 2016.03.28 |