facebook sdk not work in fragment - android

When I sign in facebook, my callback is not working.
I do not know why this part is not working: loginButton.registerCallback(callbackManager...
This is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return initView(inflater, container);
}
private View initView(LayoutInflater inflater, ViewGroup container) {
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
Log.d("FB","1");
final View view = inflater.inflate(R.layout.fragment_login, container, false);
name=(TextView)view.findViewById(R.id.FBname);
link=(TextView)view.findViewById(R.id.FBlink);
id=(TextView)view.findViewById(R.id.FBid);
LoginButton loginButton = (LoginButton)view.findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
accessToken = loginResult.getAccessToken();
Log.d("FB","access token got.");
//send request and call graph api
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("FB","complete");
Log.d("FB",object.optString("name"));
name.setText("name:" + object.optString("name"));
Log.d("FB", object.optString("link"));
link.setText("link:"+object.optString("link"));
Log.d("FB", object.optString("id"));
id.setText("id:"+object.optString("id"));
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
Log.d("FB", "CANCEL");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.d("FB",exception.toString());
}
});
return view;
}
Thanks for all respondents

did you added this to your activity?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

Related

Not getting a callback in onActivityResult while trying to integrate Facebook Login

I have been trying to integrate Facebook login with Android, login to facebook happens, however I am not getting a callback in onActivityResult. I have already followed this:
Facebook Login CallbackManager FacebookCallback not called?
However, my problem remains unsolved. Here is a code snippet:
#Override
protected FragmentPresenter getFragmentPresenter() {
return presenter;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(getLayoutId(), container, false);
setHasOptionsMenu(true);
view.setClickable(true);
setupView(view);
//FacebookSdk.sdkInitialize(FacebookSdk.getApplicationContext());
FacebookSdk.sdkInitialize(getContext());
callbackManager = CallbackManager.Factory.create();
loginFacebook.setReadPermissions(Arrays.asList(EMAIL));
loginFacebook.setFragment(this);
loginFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
getActivity().setResult(RESULT_OK);
getActivity().finish();
}
#Override
public void onCancel() {
getActivity().setResult(RESULT_CANCELED);
getActivity().finish();
}
#Override
public void onError(FacebookException exception) {
//Handle Exception
}
});
return view;
}
Please help me.

ACCESS_TOKEN_REMOVED Facebook 4.8.0 SDK Android

My App logs into facebook but when I check the token it returns this error: {accessToken token: ACCESS_TOKEN_REMOVED permissions: [user_friends, contact_email, email, public_profile]}.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
View view = inflater.inflate(R.layout.login, container, false);
authButton = (LoginButton)view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("email"));
progress = (ProgressBar) view.findViewById(R.id.progressBar);
progress.setVisibility(View.INVISIBLE);
editEmail = (EditText) view.findViewById(R.id.editEmail);
editSenha = (EditText) view.findViewById(R.id.editSenha);
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
MyLog.log("Logged in!...");
makeMeRequest(loginResult);
}
#Override
public void onCancel() {
MyLog.log("Logged out...");
progress.setVisibility(View.INVISIBLE);
}
#Override
public void onError(FacebookException exception) {
MyLog.log("Logged out...");
progress.setVisibility(View.INVISIBLE);
}
});
return view;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Can someone help me?

Android Facebook Login onActivityResult

I use fragment for facebook login. Normally everything goes fine. But in first time , when user login with facebook s/he redirected to facebook page and give permission to program but then (despite i use CallbackManager.onActivityResult) , program cannot handle the callback and stay background. How can i handle the callback ??
Relative code snippet:
public void onCreate(Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
super.onCreate(savedInstanceState);
callbackManager = CallbackManager.Factory.create();
}
public void onResume() {
Log.i("tago" , "onResume");
super.onResume();
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
tumisim = profile.getName();
firstname = profile.getFirstName();
lastname = profile.getLastName();
sharedtumisimkaydet(tumisim);
sharedfirstnamekaydet(firstname);
sharedlastnamekaydet(lastname);
}
}
public void onStop() {
super.onStop();
getActivity().finish();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.facebookfragment, container, false);
int[] taniticiresimler = {R.mipmap.aciklama,R.mipmap.dene_uc,R.mipmap.yenigus,R.mipmap.galp};
CustomPagerAdapter pagerAdapter = new CustomPagerAdapter(getActivity(),taniticiresimler);
FadingIndicator indicator = (FadingIndicator)view.findViewById(R.id.circleIndicator);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager);
viewPager.setAdapter(pagerAdapter);
indicator.setViewPager(viewPager);
indicator.setFillColor(Color.BLUE);
indicator.setStrokeColor(Color.BLACK);
indicator.setRadius(15f);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("user_friends", "public_profile", "email"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
profile = Profile.getCurrentProfile();
if (profile.getId() != null) {
facebookID = profile.getId();
String a = sharedFacebookIDAl();
if (a.equals("defaultfacebookID")) {
sharedilkgiriskaydet(true);
}else if(!a.equals(facebookID)){
sharedilkgiriskaydet(true);
}else{
sharedilkgiriskaydet(false);
}
sharedfacebookIDkaydet(facebookID);
KullaniciProfilCek kPC = new KullaniciProfilCek();
kPC.execute(profile.getId());
}
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
email = object.getString("email");
sharedemailkaydet(email);
cinsiyet = object.getString("gender");
sharedcinsiyetkaydet(cinsiyet);
coverphotourl = object.getJSONObject("cover").getString("source");
sharedcoverphotourlkaydet(coverphotourl);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email,gender,cover");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
Toast.makeText(getActivity(), "Facebook Login iptal edildi", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(getActivity(), "Facebook Login hata oluşturdu", Toast.LENGTH_LONG).show();
}
});
return view;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
Log.i("tago" , "geri geldi");
}
Inside the fragment you cannot listen onActivityResult directly. Listen the results in the onActivityResult inside your Activity. Put the code below in the Activity which holds the fragment.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(childTag);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, intent);
}
}
and then the OnActivityResult() inside the fragment will work.
You are using onActivityResult() wrong - you do not call super class implementation unless you know you need to. This got nothing with the need of calling super i.e. in onCreate() etc. Here's documentation you should read now:
https://developer.android.com/training/basics/intents/result.html

Trouble with Facebook API LoginButton

Im trying to log in with facebook on my application, but when I click the login button it opens a window saying "not logged in"
I am recieve these two errors
E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
E/SysUtils: ApplicationContext is null in ApplicationStatus
Here is my code:
public class fragment_fb_btn extends Fragment {
...
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
View view = inflater.inflate(R.layout.fragment_fb_btn, container, false);
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {}
#Override
public void onCancel() {}
#Override
public void onError(FacebookException exception) {}
});
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Thanks, if anyone could tell me how to fix this I would be very happy

Unable to get Access Token in Android App which uses facebook api

I need help regarding accessToken of the facebook, the problem here is when i try to call accessToken method it returns a null. Actually i want to post comments from my app to the specific page and get comments from the page-id where these posts are posted.
The problem i have got is when i try to post comment i get an error like this An active access token must be used to query information about the current user.
and when i tried to get comments i get this error An access token is required to request this resource
I need help as i got stuck here.
Thanks for the help!!
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
linearLayout2 = (LinearLayout) inflater.inflate(R.layout.second_fragment, container, false);
editText = (EditText) linearLayout2.findViewById(R.id.commentBox);
button = (Button) linearLayout2.findViewById(R.id.submitButton);
openfeed = (LinearLayout) linearLayout2.findViewById(R.id.fakebutton);
initInstances();
initCallbackManager();
refreshButtonState();
getAllComments();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle params = new Bundle();
params.putString("message", editText.getText().toString());
GraphRequest graphRequest = new GraphRequest(
accessToken,
"/me/feed",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
System.out.println("Success: " + response);
}
}
);
graphRequest.executeAsync();
}
});
return linearLayout2;
}
private void initInstances() {
System.out.println("I am init");
openfeed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithPublishPermissions(getActivity(), Arrays.asList("publish_actions,publish_pages,manage_pages,user_about_me"));
}
});
}
private void initCallbackManager() {
System.out.println("i am initCallbackManager");
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
refreshButtonState();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
}
private void refreshButtonState() {
System.out.println("refresh button state");
if (!isLoggedIn()) {
openfeed.setVisibility(View.VISIBLE);
} else {
openfeed.setVisibility(View.GONE);
}
}
private boolean isLoggedIn() {
accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
public void getAllComments() {
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/{page-id}/feed",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
System.out.println("comments: " + response);
}
}
).executeAsync();
}
}

Categories

Resources