Permission was set in Facebook application dashboard to access user_birthday. In application user_birthday permission is listed in permission request.
request.setPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_work_history"));
In the JSON response after connecting with facebook, the tag firstname, middlename, lastname, email,and id was output but the json has no birthday tag. What have i missed
Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser graphUser, Response response) {
Log.i(TAG, "LoggedInUser:" + graphUser);
Log.i(TAG, "LoggedInUser_Id:" + graphUser.getId());
Log.i(TAG, "LoggedInUser_First:" + graphUser.getFirstName());
Log.i(TAG, "LoggedInUser_Middle:" + graphUser.getMiddleName());
Log.i(TAG, "LoggedInUser_Last:" + graphUser.getLastName());
Log.i(TAG, "LoggedInUser_Email:" + graphUser.asMap().get("email"));
Log.e(TAG, "LoggedInUser_BIRTHDAY:" + graphUser.asMap().get("user_birthday"));
String email = (String) graphUser.asMap().get("email");
if(email==null || email.isEmpty())
Toast.makeText(LogIn.this, "Failed to get facebook credentials",
Toast.LENGTH_LONG).show();
else
editEmail.setText(email);
firstName = graphUser.getFirstName();
lastName = graphUser.getLastName();
}
});
use this code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
String email = object
.optString("email");
String dob = object
.optString("birthday");
// user_id = object.optString("id");
String name = object.optString("name");
Log.d("fb detail", "" + email + " "
+ name + " " + dob);
new LoginAsynTask(context).execute(
"social", name, email, dob,
object.optString("first_name"),
object.optString("last_name"));
Toast.makeText(context, "" + email,
Toast.LENGTH_LONG).show();
}
});
Bundle parameters = new Bundle();
parameters
.putString("fields",
"id,name, email, birthday,gender,first_name,last_name");
request.setParameters(parameters);
request.executeAsync();
Related
I'm using the FB Sdk to let a user login through FB but on clicking the Continue with Facebook button the screen is getting stuck at Loading... Below is the screenshot.
This is the code I'm using https://stackoverflow.com/a/29379794
What could be the reason that the screen is stuck at Loading?
Use this below code for Facebook Login :
login.setLoginBehavior(LoginBehavior.WEB_ONLY);
login.setReadPermissions(Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends"));
login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.e("ACESS_ON_SUCCESS---", "" + loginResult.getAccessToken());
token = loginResult.getAccessToken();
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.e("ACESS_ON_COMPLETED--", "" + AccessToken.getCurrentAccessToken());
Log.e("FaceBook Response", response.toString());
String responseStr = response.toString();
sharedpreferences = App.getInstance().getDefaultAppSharedPreferences();
editor = sharedpreferences.edit();
try {
if (responseStr.contains("\"id\"")) {
id = object.getString("id");
Log.e("USER-ID = ", id);
editor.putString(SharedPreferencesConstants.facebookId,id);
}
if (responseStr.contains("\"name\"")) {
name = object.getString("name");
Log.e("NAME = ", name);
editor.putString(SharedPreferencesConstants.userName,name);
}
if (responseStr.contains("\"email\"")) {
email = object.getString("email");
Log.e("EMAIL = ", email);
editor.putString(SharedPreferencesConstants.userEmail,email); }
if (responseStr.contains("\"birthday\"")) {
birthday = object.getString("birthday");
Log.e("BIRTHDAY = ", birthday);
editor.putString(SharedPreferencesConstants.userFbBirthday,birthday);
String strYear = birthday.substring(6, 10);
Log.e("BIRTHDAY LEN", strYear);
editor.putString(SharedPreferencesConstants.userFbBirthdayYear,strYear);
}
//EXTRACT PICTUREOBJECT FROM MAINJSONOBJECT::::---
if (responseStr.contains("\"picture\"")) {
JSONObject pictureObject = object.getJSONObject("picture");
Log.e("PICTUREOBJ FRM MAINOBJ", "" + pictureObject.toString());
//EXTRACT DATAOBJECT FROM PICTUREOBJECT::::---
JSONObject dataObject = pictureObject.getJSONObject("data");
//EXTRACT PROFILE_PIC FROM DATAOBJECT::::---
profile_pic = dataObject.getString("url");
Bundle params = new Bundle();
parameters.putString("limit", total_friends_count);
request1.setParameters(params);
request1.executeAsync();*/
}
#Override
public void onCancel() {
Log.e("Error","canceled");
}
#Override
public void onError(FacebookException error) {
Log.e("Error",error.getMessage());
Toast.makeText(WelcomeActivity.this, "FB Integration Failed", Toast.LENGTH_SHORT).show();
}
});
}
I'm currently creating a test application to test using the latest facebook SDK to update our existing application problem is that I need to get the user birthday. I'm confused on this since the the SDK3 and above provides more information than the updated SDK4 and I'm lost on how to get the birthday as all the answers I've seen so far doesn't provide the birthday on my end. Here's my code so far:
LoginButton CallBacks:
fbLogin = (LoginButton) findViewById(R.id.login_button);
fbLogin.setReadPermissions(Arrays.asList("email", "user_birthday"));
fbLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
if (object.has("id"))
id = object.getString("id");
if (object.has("name"))
userName = object.getString("name");
if (object.has("email"))
userEmail = object.getString("email");
if (object.has("gender"))
gender = object.getString("gender");
if (object.has("birthday"))
birthday = object.getString("birthday");
String profile_URL = "https://graph.facebook.com/" + id+ "/picture?type=large";
LogCat.show(id + "\n" + userName + "\n" + userEmail + "\n" + birthday + "\n" + profile_URL + "\n" + gender);
} catch (Exception e) {
e.printStackTrace();
LogCat.show("Error:" + e.getMessage());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
Toast.makeText(SplashLogin.this, "CANCEL", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(SplashLogin.this, "" + exception.toString(), Toast.LENGTH_SHORT).show();
}
});
The JSON response returns only the ID and name, email and gender of my account but doesn't include the birthday. Did I missed out something?
According to the latest sdk of facebook, to get birthday you need to first submit your application for review. On test applications you can just get the public profile which includes the following things
id
cover
name
first_name
last_name
age_range
link
gender
locale
picture
timezone
updated_time
verified
For more information you can read the following documentation from this link
If your app requests this permission Facebook will have to review how
your app uses it. link
Facebook don't give user birthday by default.You have to use extended permissions.
I solve this problem by enable permission of Birthday in my app from the Facebook Developer Console.
fbSignUp.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Data.fbAccessToken = loginResult.getAccessToken().getToken();
LogCat.show(loginResult.getAccessToken().getToken());
if (object.has("id")) {
id = object.getString("id");
Data.fbId = object.getString("id");
}
if (object.has("name")) {
userName = object.getString("name");
;
Data.fbUserName = object.getString("name");
}
if (object.has("email")) {
userEmail = object.getString("email");
Data.fbUserEmail = object.getString("email");
}
if (object.has("birthday")) {
userBirthDay = object.getString("birthday");
}
else {
if (userEmail == null || "".equalsIgnoreCase(userEmail)) {
if (userName != null && !"".equalsIgnoreCase(userName)) {
userEmail = userName + "#facebook.com";
Data.fbUserEmail = Data.fbUserName + "#facebook.com";
} else {
userEmail = id + "#facebook.com";
Data.fbUserEmail = Data.fbId + "#facebook.com";
}
}
}
} catch (Exception e) {
e.printStackTrace();
LogCat.show("Error:" + e.getMessage());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, email,birthday,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
Toast.makeText(RegisterScreen.this, "CANCEL", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(RegisterScreen.this, "" + exception.toString(), Toast.LENGTH_SHORT).show();
}
});
My application allows the user to connect with Facebook. I want to get userprofile information after login with Facebook (using Facebook SDK 4.0.1). I think that I should use aynctask to get it but I don't know how.
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
#Override
public void onSuccess(LoginResult loginResult)
{
System.out.println("onSuccess");
msginfo.setText("You can now share image on facebook");
otherview.setVisibility(View.VISIBLE);
GraphRequest request = GraphRequest.newMeRequest
(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
#Override
public void onCompleted(JSONObject object, GraphResponse response)
{
// Application code
Log.v("LoginActivity", response.toString());
//System.out.println("Check: " + response.toString());
try
{
String id = object.getString("id");
idT.setText(object.getString("id"));
ppv.setProfileId(object.getString("id"));
nameT.setText(object.getString("name"));
emailT.setText(object.getString("email"));
locationT.setText(object.getString("address"));
String name = object.getString("name");
String email = object.getString("email");
String gender = object.getString("gender");
String birthday = object.getString("birthday");
// String location = object.getString("location");
// String location = object.getString("user_location");
// String location = object.getString("address");
System.out.println(id + ", " + name + ", " + email + ", " + gender + ", " + birthday);
// locationT.setText(location);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday,link");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel()
{
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception)
{
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
And I can get the profile informations in the current activity but not in the next one.
I think you are missing permissions
parameters.putString("fields", "id,name,email,gender,picture,birthday");
And then you can easily get profile pic
object.getJSONObject("picture").getJSONObject("data").getString("url");
Hope it helps.
I am using facebook sdk in my app and fetching details like facebook_id, dob, email_id, user name etc. I am getting all these details and using them in my app but the thing is that now I want to fetch interests of the logged in user but I am not getting the same, don't know what is happening.
My code for fetching all these details is as follows :-
Session.openActiveSession(context, true, new Session.StatusCallback() {
#Override
public void call(final Session session, SessionState state, Exception exception) {
// TODO Auto-generated method stub
if(session.isOpened())
{
showToast("Please wait");
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(context, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
}
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
if(user!=null)
{
try{
Log.d("", "Fb responcse == "+response.toString());
Log.d("", "FbUser >> "+ user.toString());
fbUserid=user.getId();
fbAccessToken=session.getAccessToken();
Log.i("User Name ", "FbName : "+user.getName());
Log.i("User Id ", "FbId : "+user.getId());
Log.i("User Id ", "FbLocation : "+user.getLocation().toString());
Log.i("User Id ", "FbEmailId : "+user.getLink());
Log.i("User Id ", "FbDob : "+user.getBirthday());
Log.i("User Id ", "FbGender : "+user.asMap().get("gender").toString());
Log.i("User Id ", "FbEmail : "+user.getProperty("email").toString());
//Log.i("User Id ", "FbCity : "+user.getLocation().getCity().toString());
//Log.i("User Id ", "FbState : "+user.getLocation().getState().toString());
Log.i("User Access Token ", "FbAccess Token : "+session.getAccessToken());
Log.i("USER LIKES", "USER LIKES" + user.getLink().toString());
String fqlQuery = "SELECT music, books, movies FROM user where uid ="+user.getId();
Bundle bundle = new Bundle();
bundle.putString("q", fqlQuery);
Request request = new Request(session, "/fql", bundle, HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
Log.i("INFO", response.toString());
}
});
Request.executeBatchAsync(request);
pBar.setVisibility(View.INVISIBLE);
String name = user.getName();
String dob = user.getBirthday();
gender = user.asMap().get("gender").toString();
if(gender.equalsIgnoreCase("male")){
genMale.setChecked(true);
//genFemale.setSelected(false);
//Toast.makeText(context, "male", 0).show();
}
else{
//genMale.setSelected(false);
genFemale.setChecked(true);
//Toast.makeText(context, "female", 0).show();
}
String email =user.getProperty("email").toString();
String city = user.getLocation().getProperty("name").toString();
String url="http://graph.facebook.com/"+user.getId()+"/picture?type=large";
setText(name, dob, email, city, "","",url);
userIdFB = user.getId();
accessTokenFB = session.getAccessToken();
}catch(Exception ex){
ex.printStackTrace();
}
}
else{
pBar.setVisibility(View.INVISIBLE);
Log.i("ELSE", "ELSE");
}
}
});
//session.getAccessToken();
//Toast.makeText(context, "Session AccessToken : "+session.getAccessToken(), Toast.LENGTH_LONG).show();
}
if(session.isClosed()){
//Toast.makeText(context, "Closed", 0).show();
}
}
});
So the problem is that what should I add in this so that I can get all the interests of my current logged in user.
Please help me to solve this any help would be appreciable.
Thanks in advance.
Here is my code for facebook me request.i want to get user email and other basic info.this code is work without any issue in emulator.but in real device email gives null value. id,fristname... comes with real values. how can i get email on real device?
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
RequestParams params = new RequestParams();
params.put("email",(String) user.getProperty("email"));
params.put("password", "");
params.put("facebook_id", user.getId());
editor.putString("facebook_id", user.getId());
login(params,
(String) user.asMap().get("email"),
user.getId(), user.getFirstName(),
user.getLastName());
}
}
});
Bundle params = new Bundle();
params.putString("fields", "id,email,first_name,last_name");
request.setParameters(params);
request.executeAsync();
Instead of submitting a ME request, use Facebook's LoginButton, and add a hook to intercept the user info:
// Intercept the facebook user returned from login
facebookLoginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
#Override
public void onUserInfoFetched(GraphUser user) {
mFacebookUser = user;
if (user != null) {
LogUtils.LOGFB(TAG, "Got a Facebook user: " + user.getFirstName() +
" " + user.getLastName() + ", email: " + user.getProperty("email"));
}
else {
LogUtils.LOGFB(TAG, "No Facebook user");
}
}
});
Then add an extra permission to get email:
facebookLoginButton.setReadPermissions(Arrays.asList(
"email", "user_birthday", ...));