[SO] 페이스북 연동을 위한 Hash Key 등록
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | C:\openssl-0.9.8k_X64\bin\openssl base64
AV4A36DcA2EO/8A2IXQSts7NwsU=
실행 결과
C:\Program Files\Java\jdk1.8.0_111\bin>
C:\Program Files\Java\jdk1.8.0_111\bin>keytool -exportcert -alias androiddebugke
y -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
WARNING: can't open config file: f:\repo\winlibs_openssl_vc11_x86/openssl.cnf
WARNING: can't open config file: f:\repo\winlibs_openssl_vc11_x86/openssl.cnf
AV4A36DcA2EO/8A2IXQSts7NwsU=
C:\Program Files\Java\jdk1.8.0_111\bin>
C:\Program Files\Java\jdk1.8.0_111\bin>keytool -exportcert -alias androiddebugke
y -keystore ~/.android/debug.keystore | openssl sha1 -binary | C:\openssl-0.9.8k
_X64\bin\openssl base64
WARNING: can't open config file: f:\repo\winlibs_openssl_vc11_x86/openssl.cnf
AV4A36DcA2EO/8A2IXQSts7NwsU=
패키지 이름
kr.co.braverokmc.facebook
페이스북 로그인 연동
https://developers.facebook.com/docs/android/getting-started
accesssToken () https://developers.facebook.com/docs/reference/android/current/class/AccessToken/#getCurrentAccessToken
[Android] 안드로이드 페이스북 로그인 10초만에 구현하기 ( Facebook Login )
[Android] 안드로이드 스튜디오에서 페이스북 로그인 연동하기
=> 성공
class MainActivity
package kr.co.braverokmc.facebook;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import org.json.JSONObject;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private CallbackManager callbackManager;
private TextView textView;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
String email, name, gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("result",object.toString());
textView.setText(object.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
// faceBookInfo();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
Log.e("LoginErr",error.toString());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
/*
public void faceBookInfo(){
GraphRequest request = GraphRequest.newMeRequest( getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("TAG","페이스북 로그인 결과" + response.toString());
try {
email = object.getString("email"); // 이메일
name = object.getString("name"); // 이름
gender = object.getString("gender"); // 성별
Log.d("TAG","페이스북 이메일 -> " + email);
Log.d("TAG","페이스북 이름 -> " + name);
Log.d("TAG","페이스북 성별 -> " + gender);
handler.post(new Runnable() {
@Override
public void run() {
textView.setText(email +" ," + name +", " +gender);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
*/
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kr.co.braverokmc.facebook.MainActivity">
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/login_button"
android:layout_marginTop="74dp"
android:id="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.braverokmc.facebook">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id">
<provider android:authorities="com.facebook.app.FacebookContentProvider1104450242996862"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
</meta-data>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
</application>
</manifest>
app
minSdkVersion 15
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.android.support:support-v4:25.3.1
repositories {
mavenCentral()
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.braverokmc.facebook">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id">
<provider android:authorities="com.facebook.app.FacebookContentProvider1104450242996862"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
</meta-data>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
</application>
</manifest>
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
repositories {
mavenCentral()
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
댓글 ( 4)
댓글 남기기