I'm trying to get a user's gender using the facebook sdk for Android. I am using parse to login/signup so I have access to all users id and access token. When I try and make a new HTTP GET request, I can only get MY information - but I need [user_x]'s information. If I use the Graph API Explorer it works just fine, but in code it doesn't work. Here's some of the code I have:
Login:
List<String> permissions = Arrays.asList("email","basic_info", "user_about_me", "user_location", "public_profile");
ParseFacebookUtils.logIn(permissions, this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (user == null) {
Toast.makeText(getApplicationContext(), "Uh Oh. The user cancelled the Facebook login.", Toast.LENGTH_SHORT).show();
} else if (user.isNew()) {
Toast.makeText(getApplicationContext(), "User's like new or something.", Toast.LENGTH_SHORT).show();
try {
user.put("facebookId", currentUser.getJSONObject("authData").getJSONObject("facebook").get("id").toString());
user.put("access_token", currentUser.getJSONObject("authData").getJSONObject("facebook").get("access_token").toString());
user.save();
} catch(Exception ex) {
Log.e("Nope", ex.toString());
}
login();
} else {
Toast.makeText(getApplicationContext(), "User just like logged in or something", Toast.LENGTH_SHORT).show();
login();
}
}
});
Request:
/* make the API call */
new Request(
ParseFacebookUtils.getSession(),
"/1376286562687763",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
Log.d(response.toString(), "");
try {
Log.d(response.getGraphObject().getProperty("gender").toString(), "");
} catch (Exception e) {
Log.e("oops", e.toString());
}
}
}
).executeAsync();
If I type that ID in the Graph API Explorer - works like a charm. If I run this code I get a null object returned. If I change that user id to "me" or my user id it works fine as well. Can anyone please help?
Related
Currently, I am using this tutorial to create the facebook login.
"https://github.com/ParsePlatform/ParseUI-Android"
The Parse user JSON didn't return me an email or gender. I also have research around but I still can't find the solution (well, at least, some other still returning null). Anyone can explain the reason and how to solve it?
facebookLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
loadingStart(false); // Facebook login pop-up already has a spinner
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(getActivity(),
Arrays.asList(
"public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser","Publish");
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(),
Arrays.asList( "public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser", "Read");
}
}
});
private LogInCallback facebookLoginCallbackV4 = new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (isActivityDestroyed()) {
return;
}
if (user == null) {
loadingFinish();
if (e != null) {
showToast(R.string.com_parse_ui_facebook_login_failed_toast);
debugLog(getString(R.string.com_parse_ui_login_warning_facebook_login_failed) +
e.toString());
}
} else if (user.isNew()) {
GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject fbUser,
GraphResponse response) {
/*
If we were able to successfully retrieve the Facebook
user's name, let's set it on the fullName field.
*/
ParseUser parseUser = ParseUser.getCurrentUser();
if (fbUser != null && parseUser != null && fbUser.optString("name").length() > 0) {
Log.i("FbUSer", response.getJSONObject().toString());
Log.d("FbUSer-Email",fbUser.optString("email"));
Log.i("FbUSer-gender",fbUser.optString("gender"));
parseUser.put(USER_OBJECT_NAME_FIELD, fbUser.optString("name"));
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
debugLog(getString(
R.string.com_parse_ui_login_warning_facebook_login_user_update_failed) +
e.toString());
}
loginSuccess();
}
});
}
loginSuccess();
}
}
).executeAsync();
} else {
loginSuccess();
}
}
};
Logcat Section return:
{"id":"1234524124124","name":"FbUser NAme"}
I just found the answer. I just need to set the parameter so I can get the data I want.
GraphRequest request = GraphRequest.newMeRequest(...);
Bundle parameters = new Bundle();
parameters.putString("fields","id,name,link,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
Reference: https://stackoverflow.com/a/32579366/4961962
for more user info you have to add permissoin to access those detail like email, user_about_me at facebook developer console
select app at facebook developer and go -> app details -> Configure
App Center Permissions
Need to fetch twitter user profile data after successfully logging in using parse. Please refer below code :
ParseTwitterUtils.logIn(SignupActivity.this, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (parseUser == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
} else if (parseUser.isNew()) {
Log.d("MyApp", "User signed up and logged in through Twitter!");
} else {
Log.d("MyApp", "User logged in through Twitter!");
}
}
});
I tried to get values from Parseuser object returned after login but it is showing null.
Suggest what should I do after logging in.
Thanks
First off: Are you sure that you are logged in? To verify this, make sure that in your console there is the Log "User logged in through Twitter!", if so, you can add:
String twitter = ParseTwitterUtils.getTwitter().getScreenName();
Log.d(MainActivity.class.getSimpleName(), twitter + "");
under your else if and else block, or you can replace your code with this:
ParseTwitterUtils.logIn(SignupActivity.this, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (parseUser == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
} else if (parseUser.isNew()) {
Log.d("MyApp", "User signed up and logged in through Twitter!");
String twitter = ParseTwitterUtils.getTwitter().getScreenName();
Log.d(MainActivity.class.getSimpleName(), twitter + "");
} else {
Log.d("MyApp", "User logged in through Twitter!");
String twitter = ParseTwitterUtils.getTwitter().getScreenName();
Log.d(MainActivity.class.getSimpleName(), twitter + "");
}
}
});
If your class is not "MainActivity", type it in the Log.d.
If you are having trouble singing in, you can also try this:
user = new ParseUser();
user.setUsername("Username");
user.setPassword("password");
user.setEmail("email#example.com");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
if (!ParseTwitterUtils.isLinked(user)) {
ParseTwitterUtils.link(user, this, new SaveCallback() {
#Override
public void done(ParseException ex) {
if (ParseTwitterUtils.isLinked(user)) {
Log.d("MyApp", "Woohoo, user logged in with Twitter!");
String twitter = ParseTwitterUtils.getTwitter().getScreenName();
Log.d(MainActivity.class.getSimpleName(), twitter + "");
}
}
});
}
define "ParseUser user;" outside the onCreate method.
I want to update the Username in my parse backend with the username I am receiving from the Facebook Graph API (me Request). Receiving the name is working.
After executing the loginwithreadPermissions-Method I have an valid accesstoken but I am not able to update the username in parse.
public void Login(View view) {
Log.d("Facebook_login", "Login Executed");
ParseFacebookUtils.logInWithReadPermissionsInBackground(Facebook_login.this, Arrays.asList("user_status", "read_stream", "user_about_me", "email", "public_profile"), new LogInCallback() {
#Override
public void done(final ParseUser user, ParseException err) {
if(err!=null){
Log.d("Facebook_login", err.getMessage());
}
if (user == null) {
Log.d("Facebook_login", "Uh oh. The user cancelled the Facebook login.");
Toast.makeText(Facebook_login.this,"User is still null, Login not Worked",Toast.LENGTH_SHORT).show();
}
else {
accessToken = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
String username;
try{
username= object.getString("name");
Log.d("GraphRequestME", username);
user.put("name",username);
user.saveInBackground();
user.getUsername();
Log.d("GraphRequest",user.get("name").toString());
}
catch (Exception e){
Log.e("Execption", e.getMessage());
}
Log.d("GraphRequestME", object.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
Log.d("Facebook_login", "User logged in through Facebook!");
}
if(user!=null){
if(accessToken!=null){
Log.d("AccessToken", accessToken.getToken());
}
Intent intent = new Intent(Facebook_login.this, MainActivity.class);
Facebook_login.this.startActivity(intent);
Log.d("Facebook_login", "LoggedInSuccessful");
}
}
});
}
In the Quickstart Guide(https://parse.com/docs/android/guide#users-signing-up) from Parse it is described with user.put and later user.saveInBackground() but this isn't working here.
Can anyone help?
The username field has been deprecated with Graph API v2.0. Once all apps have been migrated, you'll not be able to get the username anymore from the Graph API.
Solved!
I figured out that I have to delete the user first and then execute this again and the new attributes got set.
Seems to me that I couldn't edit an existing user with user.put(...)
I am trying to get email address of user from ParseFacebook for android but i am getting null when i try to fetch email.
here is my code
//on Login:
List<String> permissions = Arrays.asList("email","basic_info", "user_about_me", "user_location");
ParseFacebookUtils.logIn(permissions,SignUpActivity. this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
SignUpActivity.this.progressDialog.dismiss();
if(err!=null)
Log.w("parse exception",""+err.getMessage());
if (user == null) {
Log.e("FB","Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.w("FB","User signed up and logged in through Facebook!");
//showUserDetailsActivity();
getProfileInfo();
} else {
Log.w("FB","User logged in through Facebook!");
// showUserDetailsActivity();
getProfileInfo();
}
}
});
making request:
Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
JSONObject userProfile = new JSONObject();
try {
// Populate the JSON object
try {
userProfile.put("facebookId", user.getId());
userProfile.put("name", user.getName());
} catch (Exception e) {
// TODO: handle exception
}
if (user.getProperty("email") != null) {
userProfile.put("email",(String) user.getProperty("email"));
}
if (user.getLocation().getProperty("name") != null) {
userProfile.put("location", (String) user.getLocation().getProperty("name"));
}
if (user.getProperty("gender") != null) {
userProfile.put("gender",(String) user.getProperty("gender"));
}
} catch (JSONException e) {
Log.e("Facebook parsing data","Error parsing returned user data.");
}
} else if (response.getError() != null) {
if ((response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_RETRY) || (response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_REOPEN_SESSION)) {
Log.e("Facebook parsing data","The facebook session was invalidated.");
//onLogoutButtonClicked();
} else {
Log.e("Facebook parsing data","Some other error: "+ response.getError().getErrorMessage());
}
}
}
});
request.executeAsync();
I have tried everything but where ever i do i am getting email null :(
After setting the permissions as above access the email via: user.asMap().get("email"));
You should get the email like this
String email = (String) response.getGraphObject().getProperty("email");
if(email!=null && !email.equals("")
//email variable should've email id.
Try removing the permission list in the login call.
ParseFacebookUtils.logIn(SignUpActivity.this, new LogInCallback() {
//you can add permissions on later screens.
}
Please add you this request:
Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),new Request.GraphUserCallback()
In login request condition => user.isnew()
else if (user.isNew()) {
Log.w("FB","User signed up and logged in through Facebook!");
//showUserDetailsActivity();
getProfileInfo();
}
write your block in between this and you will be able to get new user info with email also.
This perfectly works in iOS and sure will help in android.
Before the line
request.executeAsync();
add these three lines.
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
Now you will get email in your response
I am working on an application that uses Parse and I want to login using Facebook.
This is my code:
Button facebook = (Button)findViewById(R.id.facebook);
facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
ParseFacebookUtils.logIn(signUpStep2.this, new LogInCallback() {
public void done(ParseUser user, ParseException err){
if (user == null) {Log.d("My app", "NULL");}
} else if (user.isNew()){Log.d("My app", "New User");}
else{Log.d("My app", "User");}
However, I am always getting a Null user, and a Null ParseException err.
What is my error?
try to set permission on login method.
you can also check these links: Android Facebook login returns null user and ParseFacebookUtils.LogIn I keep getting a null ParseUser object
ParseFacebookUtils.logIn(Arrays.asList("email",Permissions.Friends.ABOUT_ME,Permissions.Friends.BIRTHDAY),
this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d("My app", "NULL");
} else if (user.isNew()) {
Log.d("My app", "New User");
} else {
Log.d("My app", "User");
}
}
});