I've made lots of attempts to get the city for this account to view the account on the app but it dont work
//Login Facebook
public void Login()
{
Dialog dialog = new Dialog(Home.this);
dialog.setContentView(R.layout.customdialog_login);
login_button = (LoginButton)dialog.findViewById(R.id.login_button);
login_button.setReadPermissions("public_profile", "email", "user_birthday", "user_friends","user_location");
callbackManager = CallbackManager.Factory.create();
dialog.show();
login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(final JSONObject object, GraphResponse response) {
try {
SaveSystem.SaveStringData("userid" , object.getString("id"));
SaveSystem.SaveStringData("first_name" , object.getString("first_name"));
SaveSystem.SaveStringData("last_name" , object.getString("last_name"));
SaveSystem.SaveStringData("gender" , object.getString("gender"));
SaveSystem.SaveStringData("admin" , "0");
SaveSystem.SaveStringData("ban" , "0");
SaveSystem.SaveStringData("type" , "facebook");
//CheckUser
final RequestQueue requestQueue = Volley.newRequestQueue(Home.this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, Information.URL_CheckUsers + SaveSystem.LoadStringData("userid"), new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
Toast.makeText(Home.this, "Location " + object.getJSONObject("location").getJSONObject("location").getString("city"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
if(response.equals("0"))
{
//Add Account
String URL_AddUser = Information.URL_AddUsers + SaveSystem.LoadStringData("userid") + "&username=" + SaveSystem.LoadStringData("first_name") + " " + SaveSystem.LoadStringData("last_name") + "&gender=" + SaveSystem.LoadStringData("gender") + "&admin=" + SaveSystem.LoadStringData("admin") + "&ban=" + SaveSystem.LoadStringData("ban") + "&type=" + SaveSystem.LoadStringData("type");
StringRequest stringRequest1 = new StringRequest(Request.Method.GET, URL_AddUser , new Response.Listener<String>() {
#Override
public void onResponse(String response) {
setAccountData();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Home.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest1);
}
else
{
setAccountData();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location{location}");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
}
here I try to present the area to experience
try {
Toast.makeText(Home.this, "Location " + object.getJSONObject("location").getJSONObject("location").getString("city"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
can I view Json facebook on Chrome ?? , I do not know how to arrange Json on facebook
I searched a lot but I dont get the city. Is it a problem in my blades or what please help me really thank you
Related
I have a problem with old devices , when the data is sent to the server using an old device api 15 + 16 + 17 in Spanish or Portuguese, question marks will appear, and the new devices have no problems
txt_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edit_post.length() > 0)
{
txt_ok.setVisibility(View.GONE);
pd_newpostloading.setVisibility(View.VISIBLE);
String url = Information.URL_AddPendingPosts + Information.userid + "&authorname=" + Information.username + "&message=" + edit_post.getText().toString() + "&type=" + edit_category.getText().toString() + "&date=" + nowAsString;
try {
encodedValue = URLEncoder.encode(url,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
RequestQueue requestQueue = Volley.newRequestQueue(ShowMessages.this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, encodedValue, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
txt_ok.setVisibility(View.VISIBLE);
pd_newpostloading.setVisibility(View.GONE);
Toast.makeText(ShowMessages.this, "Done", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
txt_ok.setVisibility(View.VISIBLE);
pd_newpostloading.setVisibility(View.GONE);
Toast.makeText(ShowMessages.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
}
else
{
Toast.makeText(ShowMessages.this, "Empty", Toast.LENGTH_SHORT).show();
}
}
});
I hope to get some help thanks in advance :)
This is excerpt of my code. Is there any problem with the way I am setting read permissions or in applying graph api request?
LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
ArrayList<String> perList=new ArrayList<>();
perList.add("id");
perList.add("email");
//loginButton.setReadPermissions("id","email","user_birthday","user_location");
loginButton.setReadPermissions(perList);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(),"facebook login success",Toast.LENGTH_SHORT).show();
AccessToken at=loginResult.getAccessToken();
String userId=loginResult.getAccessToken().getUserId();
Ion.with(getApplicationContext()).load("graph.facebook.com/"+userId+"/picture?type=large").setTimeout(60*1000)
.asJsonObject().setCallback(new FutureCallback<JsonObject>() {
#Override
public void onCompleted(Exception e, JsonObject result) {
Toast.makeText(getApplicationContext(),"inOnComoplete",Toast.LENGTH_SHORT).show();
}
});
}
try this
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile, email"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.e("TESTCHK", "1");
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.e("DAta", response.toString());
Log.e("TESTCHK", "2");
try {
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("img", object.getJSONObject("picture").getJSONObject("data").getString("url"));
intent.putExtra("name", "Name is : " + object.getString("name") + "\nEmail is : " + object.getString("email"));
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle bundle = new Bundle();
//bundle.putString("fields", "id,name,email,gender,birthday");
bundle.putString("fields", "name,email,picture.type(large)");
request.setParameters(bundle);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Toast.makeText(MainActivity.this, "Error while login with facebook", Toast.LENGTH_SHORT).show();
}
});
ask me in case of any query
I am implementing facebook login, but I would like to take some data to another registration activity and I need the country, city, state (all) of the user. I researched but still could not understand how to bring this data.
Here is the code I'm using:
private void facebookLogin() {
mAuth = FirebaseAuth.getInstance();
mCallbackManager = CallbackManager.Factory.create();
//Login com facebook arrumar um lugar melhor e mais organizado..
facebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("JSON", "" + response.getJSONObject().toString());
try {
nome = object.optString("first_name");
sobrenome = object.optString("last_name");
email = object.optString("email");
aniversario = object.optString("user_birthday");
idFB = object.optString("id");
sexo = object.getString("gender");
paisLogin = object.getJSONObject("location").getString("country"); //????????????
cidade = object.getJSONObject("location").getString("city"); //?????????????????
SaveSharedPreferences.setIdFacebook(getContext(),idFB);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields","id,first_name,last_name,email,location,gender");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
AuthCredential credential = FacebookAuthProvider.getCredential(loginResult.getAccessToken().getToken());
handleFacebookAccessToken(credential);
//handleFacebookAccessToken(loginResult.getAccessToken());
}
#Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
How can I get this data?
This works for me (see the method getLocationUser(locationID)):
private void facebookLogin() {
mAuth = FirebaseAuth.getInstance();
mCallbackManager = CallbackManager.Factory.create();
//Login com facebook arrumar um lugar melhor e mais organizado..
facebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("JSON", "" + response.getJSONObject().toString());
try {
nome = object.optString("first_name");
sobrenome = object.optString("last_name");
email = object.optString("email");
aniversario = object.optString("user_birthday");
idFB = object.optString("id");
sexo = object.getString("gender");
locationID = object.getJSONObject("location").getString("id");
getLocationUser(locationID); <<<<-----
SaveSharedPreferences.setIdFacebook(getContext(),idFB);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields","id,first_name,last_name,email,location,gender");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
AuthCredential credential = FacebookAuthProvider.getCredential(loginResult.getAccessToken().getToken());
handleFacebookAccessToken(credential);
//handleFacebookAccessToken(loginResult.getAccessToken());
}
#Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
}
private void getLocationUser(String id) {
Bundle params = new Bundle();
params.putString("location", "id");
new GraphRequest(
AccessToken.getCurrentAccessToken(),
id+"/?fields=location",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Log.e("Response 2", response + "");
try {
paisLogin = (String) response.getJSONObject().getJSONObject("location").get("country");
cidade = (String) response.getJSONObject().getJSONObject("location").get("city");
UF = (String) response.getJSONObject().getJSONObject("location").get("state");
Log.e("Location", paisLogin);
} catch (Exception e) {
e.printStackTrace();
}
}
}
).executeAsync();
}
My code is as shown below:
facebookButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(LogInActivity.this,
Arrays.asList("public_profile", "email"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
//set login status
SessionManager.get(LogInActivity.this).setLoginStatus(true);
graphRequest(loginResult);
}
#Override
public void onCancel() {
// not called
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException e) {
// not called
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
}
});
}
});
private void graphRequest(LoginResult loginResult) {
showProgressDialog();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
if (object.has("email")) {
//store session manager
email = object.getString("email");
SessionManager.get(LogInActivity.this).setEmailId(email);
}
if (object.has("id")) {
faceBookId = object.getString("id");
picUrl = "https://graph.facebook.com/" + faceBookId
+ "/picture?type=large";
//store session manager
SessionManager.get(LogInActivity.this).setProfilePic(picUrl);
// downLoadImage(url);
}
if (object.has("name")) {
//store session manager
name = object.getString("name");
SessionManager.get(LogInActivity.this).setPersonName(name);
}
downLoadImage(picUrl, name, email, "0");
// Toast.makeText(getApplicationContext(), email, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,picture.type(small)");
request.setParameters(parameters);
request.executeAsync();
}
The problem here is , I am not able to get email id even after enabling Allow friends to include my email address in Download Your Information option in my facebook profile, is there any other way to get email id?
try it
String email = "";
try {
email = URLDecoder.decode(object.optString("email")
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
I want to check in my android app if the user's email Facebook exists or not and if it's not exist, set it as "No mail found".
The code is right but when I click the Facebook button nothing happened because my inexistent email cause an error.
I have already check with my user token and my Facebook account doesn't have an email. Obviously it works perfectly without the email string.
This is my code:
loginButtonFb.setReadPermissions("user_friends", "email");
loginButtonFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
new GraphRequest(
AccessToken.getCurrentAccessToken(),
loginResult.getAccessToken().getUserId(),
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject data = response.getJSONObject();
login_name = data.getString("name");
String idFb = data.getString("id");
String mailFb = data.getString("email");
String method = "loginFb";
BackgroundTaskLogin backgroundTask = new BackgroundTaskLogin(mContext);
backgroundTask.execute(method, login_name, idFb, mailFb);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).executeAsync();
}
#Override
public void onCancel() {
Toast.makeText(mContext, "Login attempt canceled.", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException e) {
Toast.makeText(mContext, "Login attempt failed.", Toast.LENGTH_LONG).show();
}
});
I have solved the problem. I have declared String mailFb as universal String.
The other part of the code is below:
loginButtonFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
new GraphRequest(
AccessToken.getCurrentAccessToken(),
loginResult.getAccessToken().getUserId(),
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject data = response.getJSONObject();
login_name = data.getString("name");
String idFb = data.getString("id");
if(!data.isNull("email")){
mailFb = data.getString("email");
}
mailFb = "Mail not found";
String method = "loginFb";
BackgroundTaskLogin backgroundTask = new BackgroundTaskLogin(mContext);
backgroundTask.execute(method, login_name, idFb, mailFb);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).executeAsync();
}