ParsefacebookUtils returning wrong values in Parse - android

for my android application i wanted to integrate a facebook login, so i did exactly what is here:
https://github.com/ParsePlatform/IntegratingFacebookTutorial
Everything seems to work fine however the values which are stored in the parse backend are not convenient, here is for example what i get after login with my facebook account:
emailVerified:empty
username:adGdJMxcCQCAo2.....
authData: {"facebook":{"access_token":"CAAMBDxU3HgUBAMYQ8q2mnjDqeKBz2sw......","expiration_date":"...","id":"1015353..."}}
Sorry i still can't upload pictures..
Is this normal?
i'm using parseFacebookUtils 1.9.4
parse 1.9.4

This is what i had done for facebook parse.com login
LoginFragment.java
private ParseLoginConfig config;
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState, 1);
rootView = inflater.inflate(R.layout.layout_login, container, false);
config = ParseLoginConfig.fromBundle(getArguments(), getActivity());
return rootView;
}
private void loginUsingFacebook() {
// TODO Auto-generated method stub
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(
getActivity(), Arrays.asList("public_profile", "email"),
facebookLoginCallbackV4);
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(
getActivity(), Arrays.asList("public_profile", "email"),
facebookLoginCallbackV4);
}
}
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.
*/
Log.e("facebook User", fbUser.toString());
final ParseUser parseUser = ParseUser
.getCurrentUser();
if (fbUser != null
&& parseUser != null
&& fbUser.optString("name").length() > 0) {
parseUser.put(USER_OBJECT_NAME_FIELD,
fbUser.optString("name"));
parseUser.put(USER_OBJECT_EMAIL_FIELD,
fbUser.optString("email"));
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());
}
ParseInstallation installation = ParseInstallation
.getCurrentInstallation();
installation
.put(BaseFragment.INSTALLATION_UNIQUE_ID,
parseUser
.getUsername());
installation
.saveInBackground();
loginSuccess();
}
});
}
}
}).executeAsync();
}
}
};
Note : Remove toast messages and and text me if any query!!

Related

Facebook Checkin from Android App

I am trying to perform a Facebook Checkin from an Android app that I am developing. I know we have to use the graph API. But I am not sure what all are required to get it working. Is there an example code for this?
I reviewed this link: Publishing checkins on facebook through android app
How do I use this code? Any help on this is appreciated.
public class FBCheckin extends Fragment implements View.OnClickListener {
Button checkin;
public String pageID;
String FACEBOOK_LINK = "https://www.facebook.com/pizzaguydelivery";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View rootview = inflater.inflate(R.layout.fblogin, container, false);
checkin.setOnClickListener(this);
return rootview;
}
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithPublishPermissions(getActivity(), Arrays.asList("publish_actions"));
pageID = FACEBOOK_LINK.substring(FACEBOOK_LINK.lastIndexOf("/") + 1, FACEBOOK_LINK.length());
new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + pageID, null, HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
try {
if (response.getError() == null) {
JSONObject obj = response.getJSONObject();
if (obj.has("id")) {
pageID = obj.getString("id");
Bundle params = new Bundle();
params.putString("message", "Eating");
params.putString("place", pageID);
// params.putString("link", "URL");
if (pageID == null) {
//Toast.makeText(getApplicationContext(),"Failed to check in",Toast.LENGTH_SHORT).show();
} else {
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/feed", params, HttpMethod.POST, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
if (response.getError() == null) {
//success
} else {
//Toast.makeText(getApplicationContext(),"Failed to check in",Toast.LENGTH_SHORT).show();
}
}
}).executeAsync();
}
}
}
} catch (JSONException q) {
// TODO Auto-generated catch block
//Toast.makeText(getApplicationContext(),q.getMessage(),Toast.LENGTH_SHORT).show();
q.printStackTrace();
}
}
}).executeAsync();
}
}
The following code only checks in to locations with facebook pages via pageid. ( Like restaurants.) But think you can do it with place id too.
LoginManager.getInstance().logInWithPublishPermissions(
getActivity(), Arrays.asList("publish_actions"));
String PAGEID = FACEBOOK_LINK.substring(
FACEBOOK_LINK.lastIndexOf("/") + 1,
FACEBOOK_LINK.length());
new GraphRequest(AccessToken.getCurrentAccessToken(), "/"
+ PAGEID, null, HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
try {
if (response.getError() == null) {
JSONObject obj = response
.getJSONObject();
if (obj.has("id")) {
pageID = obj.getString("id");
/* make the API call */
Bundle params = new Bundle();
params.putString("message", "Eating");
params.putString("place", pageID);
params.putString("link","URL");
if (pageID == null) {
MessageBox
.Show(getActivity(),
"Failed to check in to this restaurant");
} else {
new GraphRequest(AccessToken
.getCurrentAccessToken(),
"/me/feed", params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(
GraphResponse response) {
if (response.getError() == null) {
//success
} else {
MessageBox
.Show(getActivity(),
"Failed to check in to this restaurant");
}
}
}).executeAsync();
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
MessageBox.Show(getActivity(),
"Error: " + e.getMessage());
e.printStackTrace();
}
}
}
}).executeAsync();
I know this code is a bit messy but it get the work done. Here the first thing that I am doing is asking user for facebook publish permission. Then I check if the pageID that I am checking into is valid through a graph request. If it is valid then I am checking in the user to his desired location through another request.

Parse Facebook unable get user email and gender

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

Complications with ParseFacebookUtils, reading permissions

i'm tryng to integrate facebook login in my android application. For now i get a new row in Parse Backend about the logged user which contains his real name, some weird value(ex:SnvvKEsIv6tX7...) in his userName, and an authData which is Json and contains the following values:
access_token, expiration date, id.
After debugging i got the url of the json response, it only contains two values, the name and the id, however i declared the list like this:
List permissions = Arrays.asList("public_profile", "email");
So for now the graph is only returnin two values, i thought it was due to the permissions but as i sayed i did declared. Here is my code:
public class LoginActivity extends Activity {
private Dialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FacebookSdk.sdkInitialize(getApplicationContext());
ParseFacebookUtils.initialize(this);
// Check if there is a currently logged in user
// and it's linked to a Facebook account.
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && ParseFacebookUtils.isLinked(currentUser)) {
// Go to the user info activity
showUserDetailsActivity();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
public void onLoginClick(View v) {
progressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);
List<String> permissions = Arrays.asList("public_profile", "email");
// NOTE: for extended permissions, like "user_about_me", your app must be reviewed by the Facebook team
// (https://developers.facebook.com/docs/facebook-login/permissions/)
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
progressDialog.dismiss();
if (user == null) {
Log.d("TAG", "Uh oh. The user cancelled the Facebook login.");
} 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.
*/
Log.e("facebook User", fbUser.toString());
final ParseUser parseUser = ParseUser
.getCurrentUser();
if (fbUser != null
&& parseUser != null
&& fbUser.optString("name").length() > 0) {
parseUser.put("name",
fbUser.optString("name"));
parseUser.put("email",
fbUser.optString("email"));
Log.v("isnew, values: ","values : "+fbUser.optString("name")+","+fbUser.optString("email")+","+fbUser.optString("age_range")+","+fbUser.optString("locale"));
parseUser
.saveInBackground(new SaveCallback() {
#Override
public void done(
ParseException e) {
if (e != null) {
Log.v("", (getString(R.string.com_parse_ui_login_warning_facebook_login_user_update_failed)
+ e.toString()));
}
ParseInstallation installation = ParseInstallation
.getCurrentInstallation();
installation
.put(ParseConstants.KEY_USER_ID,
parseUser
.getUsername());
installation
.saveInBackground();
showUserDetailsActivity();
}
});
}
}
}).executeAsync();
} else {
Log.d("TAG", "User not new logged in through Facebook!");
showUserDetailsActivity();
}
}
});
}
private void showUserDetailsActivity() {
Intent intent = new Intent(this, UserDetailsActivity.class);
startActivity(intent);
}
}
public class UserDetailsActivity extends Activity {
private ProfilePictureView userProfilePictureView;
private TextView userNameView;
private TextView userGenderView;
private TextView userEmailView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userdetails);
userProfilePictureView = (ProfilePictureView) findViewById(R.id.userProfilePicture);
userNameView = (TextView) findViewById(R.id.userName);
userGenderView = (TextView) findViewById(R.id.userGender);
userEmailView = (TextView) findViewById(R.id.userEmail);
//Fetch Facebook user info if it is logged
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && currentUser.isAuthenticated()) {
makeMeRequest();
}
}
#Override
public void onResume() {
super.onResume();
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// Check if the user is currently logged
// and show any cached content
updateViewsWithProfileInfo();
} else {
// If the user is not logged in, go to the
// activity showing the login view.
startLoginActivity();
}
}
private void makeMeRequest() {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
if (jsonObject != null) {
JSONObject userProfile = new JSONObject();
try {
userProfile.put("facebookId", jsonObject.getLong("id"));
userProfile.put("name", jsonObject.getString("name"));
if (jsonObject.getString("gender") != null)
userProfile.put("gender", jsonObject.getString("gender"));
if (jsonObject.getString("email") != null)
userProfile.put("email", jsonObject.getString("email"));
// Save the user profile info in a user property
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("profile", userProfile);
currentUser.saveInBackground();
// Show the user info
updateViewsWithProfileInfo();
} catch (JSONException e) {
Log.d("TAG",
"Error parsing returned user data. " + e);
}
} else if (graphResponse.getError() != null) {
switch (graphResponse.getError().getCategory()) {
case LOGIN_RECOVERABLE:
Log.d("TAG",
"Authentication error: " + graphResponse.getError());
break;
case TRANSIENT:
Log.d("TAG",
"Transient error. Try again. " + graphResponse.getError());
break;
case OTHER:
Log.d("TAG",
"Some other error: " + graphResponse.getError());
break;
}
}
}
});
request.executeAsync();
}
private void updateViewsWithProfileInfo() {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.has("profile")) {
JSONObject userProfile = currentUser.getJSONObject("profile");
try {
if (userProfile.has("facebookId")) {
userProfilePictureView.setProfileId(userProfile.getString("facebookId"));
} else {
// Show the default, blank user profile picture
userProfilePictureView.setProfileId(null);
}
if (userProfile.has("name")) {
userNameView.setText(userProfile.getString("name"));
} else {
userNameView.setText("");
}
if (userProfile.has("gender")) {
userGenderView.setText(userProfile.getString("gender"));
} else {
userGenderView.setText("");
}
if (userProfile.has("email")) {
userEmailView.setText(userProfile.getString("email"));
} else {
userEmailView.setText("");
}
} catch (JSONException e) {
Log.d("TAG", "Error parsing saved user data.");
}
}
}
public void onLogoutClick(View v) {
logout();
}
private void logout() {
// Log the user out
ParseUser.logOut();
// Go to the login view
startLoginActivity();
}
private void startLoginActivity() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
i'm using Parse 1.9.4 and ParseFacebookUtils 1.9.4.

Android Parse Login with email or username

I'm trying to make an android app using parse that allows one to login using either their username or email. I have tried querying to get an object with a provided email address, then get that object's username to login with. However, I am getting this error:
'getFirstInBackground(com.parse.GetCallback)' in 'com.parse.ParseQuery' cannot be applied to '(anonymous com.parse.GetCallback "com.parse.ParseObject>)'
I'm new to app dev and parse, so I'm not entirely sure what this means. From what I understand it wont let me use getFirstInBackground because that works with parseUser's and I'm working with ParseObjects, but the code I'm using I pulled from a prior stackoverflow question where this was a working answer: Login username AND email in Parse Android
Here is my code:
// Do this to allow for username or email log in
if (mEmail.indexOf("#") != -1) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("email", mEmail);
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
String actualUsername = object.getString("username");
ParseUser.logInInBackground(actualUsername, mPassword, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (e != null) {
// TODO: Show error message
Toast.makeText(LoginActivity.this, "Credentials incorrect", Toast.LENGTH_LONG).show();
} else {
// Start Intent for activity
// TODO: Choose activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
}
});
}
}
});
According to docs, the method's signature is as follows:
class com.parse.ParseQuery<T> {
public void getFirstInBackground(GetCallback<T> callback)
}
That means that the generic type of GetCallback must be the same as in your ParseQuery. So it probably should be
query.getFirstInBackground(new GetCallback<ParseUser>()) { /* ... */ }
You are attempting to query a ParseUser with a GetCallBack of type ParseObject.
Replace all instances of ParseObject with ParseUser in your code.
query.getFirstInBackground(new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
String actualUsername = user.getUsername();
ParseUser.logInInBackground(actualUsername, mPassword, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (e != null) {
// TODO: Show error message
Toast.makeText(LoginActivity.this, "Credentials incorrect", Toast.LENGTH_LONG).show();
} else {
// Start Intent for activity
// TODO: Choose activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
}
});
}
}
});
Hope this helps
final String userOrEmal = editTextEmailOrUserName.getText().toString().trim();
final String mpassword = editTextPassword.getText().toString().trim();
if (userOrEmal.contains("#")) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("email", userOrEmal);
query.getFirstInBackground(new GetCallback<ParseUser>() {
#Override
public void done(ParseUser object, ParseException e) {
if (object != null) {
ParseUser.logInInBackground(object.getString("username"), mpassword, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
Log.v("login", "secessful");
} else {
Log.v("login", "fail");
}
}
});
} else {
}
}
});
} else {
ParseUser.logInInBackground(userOrEmal,mpassword, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
Log.v("login", "secessful");
} else {
Log.v("login", "fail");
}
}
});
}

Unable to fetch Friendlist Birthday Facebook 3.0 android

i am getting a null when i try to fetch my friend's birthday . this is where i set the permissions
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.activity_main, container, false);
LoginButton authButton = (LoginButton) view
.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("user_likes",
"friends_birthday", "friends_about_me"));
return view;
// return super.onCreateView(inflater, container, savedInstanceState);
}
and this is how i am trying to fetch birthdays
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
Request.executeMyFriendsRequestAsync(session,
new Request.GraphUserListCallback() {
#Override
public void onCompleted(List<GraphUser> users,
Response response) {
// TODO Auto-generated method stub
// Log.i("Response JSON", response.toString());
names = new String[users.size()];
id = new String[users.size()];
for (int i = 0; i < users.size(); i++) {
names[i] = users.get(i).getBirthday();
//id[i] = users.get(i).ge();
Log.i("Birthday chakk te bai ", "Birthday"
+ names[i]);
}
}
});
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
Please see why i am geeting null . Every thing is fine with keyhash as i tested it for friends name .
App is only in the sandbox mode.
Try this-
Your callback, where you'll get friends and birthday info(if authenticated):
Session.StatusCallback statusCallback = new Session.StatusCallback()
{
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
getFriendDetails();
}
}
};
Then, open the session and set the permissions:
Session session = new Session(this);
session.openForRead(new Session.OpenRequest(this)
.setCallback(statusCallback)
.setPermissions(Arrays.asList("friends_birthday")));
As a last step, get the friends birthday and other details:
private void getFriendDetails()
{
Request myFriendsRequest = Request.newMyFriendsRequest(Session.getActiveSession(),
new Request.GraphUserListCallback()
{
#Override
public void onCompleted(List<GraphUser> users, Response response)
{
if (response.getError() == null)
{
// Print response
}
}
});
Bundle requestParams = myFriendsRequest.getParameters();
requestParams.putString("fields", "name,birthday");
myFriendsRequest.setParameters(requestParams);
myFriendsRequest.executeAsync();
}

Categories

Resources