Android uygulamama girişi entegre etmek için facebook sdk 4.10.0'ı kullandım. İzlediğim eğitim:
facebook oturum açma android stüdyosu
Facebbok'tan ad, soyad, e-posta, cinsiyet, facebook kimliği ve doğum tarihini alabileceksiniz.
Yukarıdaki eğitim, video aracılığıyla facebook geliştirici konsolunda nasıl uygulama oluşturulacağını da açıklamaktadır.
aşağıdaki build.gradle(Module:app)
dosyaya ekleyin :
repositories {
mavenCentral()
}
ve
compile 'com.facebook.android:facebook-android-sdk:4.10.0'
şimdi aşağıda AndroidManifest.xml dosyasına ekleyin:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="your app id from facebook developer console"/>
<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" />
Activity_main.xml dosyasına aşağıdakileri ekleyin:
<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"
tools:context="com.demonuts.fblogin.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/text"/>
<com.facebook.login.widget.LoginButton
android:id="@+id/btnfb"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Ve son olarak MainActivity.java dosyasına aşağıdaki ekleyin:
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import org.json.JSONException;
import org.json.JSONObject;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private TextView tvdetails;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private LoginButton loginButton;
private FacebookCallback<LoginResult> callback = 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) {
Log.v("LoginActivity", response.toString());
// Application code
try {
Log.d("tttttt",object.getString("id"));
String birthday="";
if(object.has("birthday")){
birthday = object.getString("birthday"); // 01/31/1980 format
}
String fnm = object.getString("first_name");
String lnm = object.getString("last_name");
String mail = object.getString("email");
String gender = object.getString("gender");
String fid = object.getString("id");
tvdetails.setText(fnm+" "+lnm+" \n"+mail+" \n"+gender+" \n"+fid+" \n"+birthday);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email, gender, birthday, location");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
setContentView(R.layout.activity_main);
tvdetails = (TextView) findViewById(R.id.text);
loginButton = (LoginButton) findViewById(R.id.btnfb);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker= new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
loginButton.registerCallback(callbackManager, callback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
@Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
}
}