Ref : http://www.theappguruz.com/blog/android-facebook-integration-tutorial
Add your development and release key hashes
To ensure the authenticity of the interactions between your app and Facebook, you need to supply us with the Android key hash for your development environment. If your app has already been published, you should add your release key hash too.
If your app has already been published, you should also add a hash of your release key.
Key Hashes
cd C:\Users\jacky\.android\
輸入
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\jacky\.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary | "C:\OpenSSL\bin\openssl" base64
isHAf19i0Op7Wwq5qe3+tCqPq2c=
Track App Installs and App Opens
App Events let you measure installs on your mobile app ads, create high value audiences for targeting, and view analytics including user demographics. To automatically log app activation events, add the following code to the onCreate() method of your app's Application class:
@Override public void onCreate() { super.onCreate(); FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); }
Now, when people install or engage with your app, you'll see this data reflected in your app's Insights dashboard.
FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this);
E. 啟用應用程式的單一登入
從 Facebook 開發人員網站的我的應用程式中選擇您的應用程式,接著選擇應用程式的設定,然後將單一登入設為是,以啟用應用程式的單一登入功能。
F. 將 FacebookActivity
包含在 AndroidManifest.xml
中。
<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" />
2.新增「Facebook 登入」按鈕
將「Facebook 登入」新增至您應用程式最簡單的方法是從 SDK 新增
LoginButton
。此為 Button
的自訂檢視實作。您可以使用此按鈕在您的應用程式中實作「Facebook 登入」。
連同
LoginButton
,您還可以使用 SDK 提供的下列類別:- LoginManager - 使用要求的讀取或發佈權限起始登入程序。
- CallbackManager - 用於將呼叫傳回至 Facebook SDK 以及您的已註冊回呼。您應該從起始活動或片段
onActivityResult
呼叫此函數。 - AccessToken:- 使用此類別 Graph API 要求。它會顯示用戶編號及被接受和被拒絕的權限。
- 個人檔案 - 此類別具有已登入用戶的基本資訊。
LoginButton
是包裝 LoginManager
中可用功能的 UI 元素。因此,當用戶點擊這個按鈕時,將會以LoginManager
中設定的權限初始化登入。此按鈕遵循登入狀態,並根據用戶的驗證狀態顯示正確的文字。
若要新增「Facebook 登入」按鈕,請使用完整類別名稱
com.facebook.widget.LoginButton
,先將其新增至您的配置 XML 檔案:<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" />
把這段code 加入到 activity_main.xml
然後透過將按鈕新增至片段以在您的 UI 中設定該按鈕,並更新您的活動,即可使用您的片段。
您可以自訂
Login button
的屬性並在 onCreateView()
方法中註冊回呼。
您可以自訂的屬性包括
LoginBehavior
、DefaultAudience
、ToolTipPopup.Style
以及LoginButton
上的屬性。例如:@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.splash, container, false); loginButton = (LoginButton) view.findViewById(R.id.login_button); loginButton.setReadPermissions("email"); // If using in a fragment loginButton.setFragment(this); // Other app specific specialization // Callback registration loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // App code } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); }
最後要到
https://developers.facebook.com/apps/1792045184412055/review-status/
告訴fb 說你的程式已經對外發布
另外我找到一個網站
Ref : https://www.numetriclabz.com/android-facebook-integration-and-login/#Final_Source_code
裡面有比較簡單的範例....
至於要抓出 email... 和其他資訊.....就要參考下面的作法
loginButton.setReadPermissions(Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends"));
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
// Facebook Email address
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.v("LoginActivity Response ", response.toString());
try {
Name = object.getString("name");
FEmail = object.getString("email");
Log.v("Email = ", " " + FEmail);
Toast.makeText(getApplicationContext(), "Name " + Name, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
LoginManager.getInstance().logOut();
}
@Override
public void onError(FacebookException e) {
}
};
paste 下面的code 到 onCreate
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.example.packagename",
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 (NameNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Just modify the package name.Then go to your Log cat and select Debug search here then you will find the hash key. Now copy this hash key and then go to developer.facebook.app_id site then edit your hash key then press save. Now run your android project again I think issue will be resolved.
沒有留言:
張貼留言