I'm trying to get user events (One's he's invited to) using the FB Android sdk but I have no clue how to do it and I've found no instructions on their website.
Here's my code for the meanwhile -
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
public class SocigoFragmentEntrance extends Fragment {
private final static String TAG = "FB_LOGIN";
private UiLifecycleHelper uiHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getActionBar().hide();
Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
}
});
}
};
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent
, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_entrance, parent, false);
LoginButton fbButton = (LoginButton)v.findViewById(R.id.authButton);
fbButton.setApplicationId(getResources().getString(R.string.app_id));
fbButton.setReadPermissions(new String[]{"user_events", "user_interests", "user_likes"});
fbButton.setFragment(this);
return v;
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
Working on it for a while I've successfully figured out how to do it.
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
public class FragmentEntrance extends Fragment {
private final static String TAG = "";
private UiLifecycleHelper uiHelper;
private String fqlQuery;
//This for some reason has to be a variable
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getActionBar().hide();
this.setRetainInstance(true);
//Reset the query
fqlQuery = "Empty";
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_entrance, parent, false);
LoginButton fbButton = (LoginButton) v.findViewById(R.id.authButton);
//Get permissions
fbButton.setApplicationId(getResources().getString(R.string.app_id));
fbButton.setReadPermissions(new String[] { "user_events",
"user_interests", "user_likes" });
fbButton.setFragment(this);
return v;
}
private void onSessionStateChange(final Session session,
SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "User logged in.");
//Here we get the user's ID and use it with the query
if (state != SessionState.OPENED) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
fqlQuery = "SELECT eid FROM event_member WHERE start_time >= now() AND uid = '"
+ user.getId() + "'";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Log.d(TAG, fqlQuery);
Request request = new Request(session, "/fql",
params, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(
Response response) {
if (response.getError() == null) {
//do things with the data
} else {
Toast.makeText(
getActivity(),
"Loading events failed.",
Toast.LENGTH_LONG)
.show();
}
}
});
Request.executeBatchAsync(request);
}
}
}).executeAsync();
}
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
Related
I have integrated Facebook with my app.But my requirement is after giving Facebook account access permission my app should autopost some text,images,links like below
to my facebook wall and then redirect to another activity (For example- SecondAcitivity)
FacebookActivity.java
package facebookIntegration;
import com.pcs.sliderringtineproj.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class FacebookActivity extends FragmentActivity {
private MainFragment mainFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, mainFragment)
.commit();
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MainFragment.java
package facebookIntegration;
import java.util.Arrays;
import com.facebook.SessionState;
import com.facebook.Session;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;
import com.pcs.sliderringtineproj.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback=new Session.StatusCallback(){
#Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_facebook, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
if(Session.getActiveSession().isOpened()){
Toast.makeText(getActivity(), "logged in to FB", 1).show();
}else{
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("public_profile"));
}
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
#Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
Bundle params = new Bundle();
params.putString("message", message);
String uri = "/me/feed?message=" + message + "" + "&access_token=" + session.getAccessToken();
uri = uri.replace(" ", "%20");
new Request(
session,
uri,
params,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
}
}
).executeAsync();
I am trying to integrate facebook account with my app.buy i am getting Error
App Not Setup: The developers of this app have not set up this app properly for Facebook Login.
Here is my logcat
03-23 14:19:57.784: E/Package Name=(10548): com.pcs.sliderringtineproj
03-23 14:19:57.854: E/Key Hash=(10548): UkWsiQcnfXkYq9BLp4yzhmhcnbk=
This seems to explain the issue: https://www.facebook.com/help/community/question/?id=10201756195602898
You need to make it public by going to your app in the developer console, click on Status & review and check yes for public.
MainActivity.Class
package com.example.facebooklogin;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
private MainFragment mainFragment;
private static final String TAG = "MainFragment";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, mainFragment)
.commit();
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
}
MainFragment.Class(For Facebook)
package com.example.facebooklogin;
import java.util.Arrays;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.main, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setReadPermissions(Arrays.asList("public_profile", "email",
"user_location"));
authButton.setFragment(this);
return view;
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
makeMeRequest(session);
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
private void makeMeRequest(final Session session) {
// Make an API call to get user data and define a
// new callback to handle the response.
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
/*
* #Override public void onCompleted(GraphUser user, Response
* response) { // If the response is successful if (session ==
* Session.getActiveSession()) { if (user != null) { // Set the
* id for the ProfilePictureView // view that in turn displays
* the profile picture. Log.e("user",user.toString()); } } if
* (response.getError() != null) { // Handle errors, will do so
* later. } }
*/
#Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
Log.e("user", user.toString());
Log.e("username", user.getName());
}
});
request.executeAsync();
}
#Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
Screenshot of the problem in debug:
Follwoing is my complete code of facebook login file
import java.util.ArrayList;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONObject;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
Context context;
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
// Display the parsed user info
// userInfoTextView.setText(buildUserInfoDisplay(user));
// Log.d(TAG,"Failed to login");
}
}
}).executeAsync();
}
else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private String buildUserInfoDisplay(GraphUser user) {
StringBuilder userInfo = new StringBuilder("");
// Example: typed access (name)
// - no special permissions required
userInfo.append(String.format("Name: %s\n\n", user.getName()));
// Example: typed access (birthday)
// - requires user_birthday permission
userInfo.append(String.format("Birthday: %s\n\n", user.getBirthday()));
// Example: partially typed access, to location field,
// name key (location)
// - requires user_location permission
userInfo.append(String.format("Location: %s\n\n", user.getLocation()
.getProperty("name")));
// Example: access via property name (locale)
// - no special permissions required
userInfo.append(String.format("Locale: %s\n\n",
user.getProperty("locale")));
// Example: access via key for array (languages)
// - requires user_likes permission
JSONArray languages = (JSONArray) user.getProperty("languages");
if (languages.length() > 0) {
ArrayList<String> languageNames = new ArrayList<String>();
for (int i = 0; i < languages.length(); i++) {
JSONObject language = languages.optJSONObject(i);
// Add the language name to a list. Use JSON
// methods to get access to the name field.
languageNames.add(language.optString("name"));
}
userInfo.append(String.format("Languages: %s\n\n",
languageNames.toString()));
}
return userInfo.toString();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_layout, container, false);
LoginButton authButton = (LoginButton) view
.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("user_about_me",
"user_likes", "user_status", "user_groups"));
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
public void onResume() {
super.onResume();
uiHelper.onResume();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
Any help regarding this issue will be a great favour thanks...
Try using the following code , it might help , working fine in my case.
import java.util.Arrays;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
public class MainFragment extends Fragment {
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(final Session session, final SessionState state, final Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private TextView userInfoTextView;
private String fbAccessToken;
private ImageView userProfileImg;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
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_location", "user_birthday", "user_likes"));
userInfoTextView = (TextView) view.findViewById(R.id.userInfoTextView);
userProfileImg = (ImageView)view.findViewById(R.id.user_profile_img);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
#Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
#SuppressWarnings("deprecation")
private void onSessionStateChange(final Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
userInfoTextView.setVisibility(View.VISIBLE);
// Request user data and show the results
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
userInfoTextView.setText(buildUserInfoDisplay(user,fbAccessToken));
}
}
});
}
else if (state.isClosed()) {
Toast.makeText(getActivity(), "Please Login", Toast.LENGTH_SHORT).show();
userInfoTextView.setVisibility(View.GONE);
}
}
private String buildUserInfoDisplay(GraphUser user,String token) {
StringBuilder userInfo = new StringBuilder("");
userInfo.append(String.format("Name: %s\n\n",
user.getName()));
userInfo.append(String.format("Birthday: %s\n\n",
user.getBirthday()));
userInfo.append(String.format("Id: %s\n\n",
user.getId()));
userInfo.append(String.format("Locale: %s\n\n",
user.getProperty("locale")));
userInfo.append(String.format("Link: %s\n\n",
user.getLink()));
userInfo.append(String.format("Gender:%s\n\n",user.asMap().get("gender").toString()));
userInfo.append(String.format("Token:%s\n\n",token));
return userInfo.toString();
}
}
I am doing a module of my project where i need to complete login with facebook and get basic details like state,name,email...
I followed the following tutorials
https://developers.facebook.com/docs/android/login-with-facebook
https://stackoverflow.com/questions/20744927/access-profile-details-from-facebook-login-in-android
and others..
I am able to execute them but even after showing the alert message saying this want to acess details.. The "state" is not getting created..
I got to know this by state.isOpened function..
Can any one help....
Intent it=new Intent(getApplicationContext(),FacebookLogin.class);
startActivity(it);
FacebookLogin class:
package com.firstandroid.app;
import java.util.Arrays;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
public class FacebookLogin extends Activity{
private static final String TAG = "FacebookLogin";
private UiLifecycleHelper uiHelper;
private Session session;
GraphUser user;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LoginButton authButton = (LoginButton)findViewById(R.id.authButton);
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
authButton.setReadPermissions(Arrays.asList("email"));
uiHelper = new UiLifecycleHelper(FacebookLogin.this, callback);
uiHelper.onCreate(savedInstanceState);
// String id=user.asMap().get("email").toString();
// String name= user.getLastName();
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
// user has either logged in or not ...
// make request to the /me API
Request request = Request.newMeRequest(this.getSession(),
new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user,
Response response) {
// TODO Auto-generated method stub
if (user != null) {
// TextView welcome = (TextView) findViewById(R.id.welcome);
//welcome.setText("Hello " + user.getName() + "!");
Log.e("user fname",user.getFirstName());
Log.e("user lname",user.getLastName());
Log.e("user username",user.getUsername());
Log.e("user email",(String)user.getProperty("email"));
}
}
});
Request.executeBatchAsync(request);
}
else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
public final Session getSession() {
return this.session;
}
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
#Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
You need to call onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
So when login done . It comes to same activity and calls status callback with session state is opened
initialize UiLifecycleHelper
private UiLifecycleHelper uiHelper;
in oncreate need to call before setContentView
uiHelper = new UiLifecycleHelper(this, statusCallback);
uiHelper.onCreate(savedInstanceState);
and need to maintain session in
#Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
}
#Override
protected void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
Source link:
link
Unable to open facebook session. Getting CLOSED_LOGIN_FAILED while opening the session with SSO_WITH_FALLBACK. It is not a native app. Tried in both cases ie. when 1.the user is already logged into the facebook default native app and 2.user is not logged in into the facebook default native app.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.facebook.Session;
import com.facebook.SessionLoginBehavior;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
public class PostActivity extends Activity {
Button buttonOpenSession ;
private UiLifecycleHelper uiHelper;
Session mSession;
Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
Log.d("appState", "PostActivity " + state);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
buttonOpenSession = (Button) findViewById(R.id.buttonOpenSession);
buttonOpenSession .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openSession();
}
});
}
public void openSession(){
mSession = Session.getActiveSession();
if (mSession == null) {
mSession = new Session(this.getApplicationContext());
}
if (!mSession.isOpened() && !mSession.isClosed()) {
mSession.openForRead(new Session.OpenRequest(this)
.setCallback(callback)
.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK));
} else {
mSession = Session.openActiveSession(this, true, callback);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
#Override
public void onResume() {
super.onResume();
uiHelper.onResume();
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}