I have a simple app, in which I am using facebook sdk to get user info. I am trying to share content by using ShareLinkContent.Builder but I am receving error:
onError:
{FacebookServiceException: httpResponseCode: -1, facebookErrorCode: 100, facebookErrorType: null, message: The parameter 'href' or 'media' is required}
Here is my MainActivity:
CallbackManager callbackManager;
ShareDialog shareDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.d(LOG_TAG, "success");
}
#Override
public void onError(FacebookException error) {
Log.e(TAG, "onError: ", error);
}
#Override
public void onCancel() {
Log.d(LOG_TAG, "cancel");
}
});
createPost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent.Builder post = new ShareLinkContent.Builder();
post = post.setContentUrl(Uri.parse(""));
post.setContentTitle("");
shareDialog.show(MainActivity.this, post.build());
}
}
});
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
What happens is that, dialog does not appear and I get the above error in logcat.
UPDATE:
What I want:
Is it possible to use ShareLinkContent.Builder with a null setContentUrl, I just want my user to share his text not a link or something else. Is it possible?
Related
I was following a tutorial on this exact question but the method used (Facebooksdk.sdkInitialize) appears to be deprecated. So now I'm unsure how to open a new activity after login. Here's my code:
public class MainActivity extends AppCompatActivity {
LoginButton loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
What's the most simple way to open a new activity after login?
Try this ,login button have callbacks and onSuccess you can move to next activity....
//Declare this on Top
CallbackManager mCallbackManager;
LoginButton loginButton;
//in onCreate
mCallbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
//OPEN ACTIVITY FROM HERE
startActivity(new Intent(CurrentActivity.this,DestinationActivity.class));
}
#Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
//and put this method on your activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I am creating an app in which I want to integrate facebook sdk. I want to login user with facebook, but it's creating problem.
It's working only one time. If I install the app and login with facebook it's working but when I uninstall my app and try to install and login again with facebook it's not working. It is not asking for access permission and doing nothing.
I want to ask one more question, I want to make custom facebook login button and after successfully login go to next Activity.
Here what I have already done. I don't know where is mistake?
Here is my main xml:
<ImageButton
android:id="#+id/btnFblogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onClick"
android:src="#drawable/fb_signin" />
<com.facebook.login.widget.LoginButton
android:id="#+id/fb_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
Here is my fragment :
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
btnFbLog = (ImageButton) view.findViewById(R.id.btnFblogin);
btnFbLog.setOnClickListener(this);
return view;}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnFblogin:
mSignInClicked = true;
Toast.makeText(getApplicationContext(), "Clicked on facebook login", Toast.LENGTH_SHORT).show();
fbloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
//updateFacebookButtonUI();
Profile profile = Profile.getCurrentProfile();
loginSocial(profile.getId(), Const.SOCIAL_FACEBOOK);
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
public void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
private void loginSocial(String id, String loginType) {
if (!AndyUtils.isNetworkAvailable(activity)) {
AndyUtils.showToast(getResources().getString(R.string.no_internet),
activity);
return;
}
AndyUtils.showCustomProgressDialog(activity,
getResources().getString(R.string.text_signin), false, null);
HashMap<String, String> map = new HashMap<String, String>();
map.put(Const.URL, Const.ServiceType.LOGIN);
map.put(Const.Params.SOCIAL_UNIQUE_ID, id);
map.put(Const.Params.DEVICE_TYPE, Const.DEVICE_TYPE_ANDROID);
map.put(Const.Params.DEVICE_TOKEN,
new PreferenceHelper(activity).getDeviceToken());
map.put(Const.Params.LOGIN_BY, loginType);
new HttpRequester(activity, map, Const.ServiceCode.LOGIN, this);
}
You can easily create your custom facebook login button via com.facebook.login.LoginManager. Facebook button is only wrapper for this class.
Sample:
private void loginWithFacebook() {
if(AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile() != null) {
//logout
LoginManager.getInstance().logOut();
} else {
//login
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(ActivityLogin.this, "onSuccess: " + loginResult.getAccessToken().getUserId(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
Toast.makeText(ActivityLogin.this, "onCancel", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(ActivityLogin.this, "onError: " + error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I'm using Facebook SDK 4+ and my problem is that when i use
FacebookSdk.sdkInitialize(getApplicationContext());
LoginManager.getInstance().logOut();
to logout from my app it's not working correct. I return to my target activity with the login button. When i press the button i get in the app again without authorization. I have installed the native Facebook app. Without this app erverything is working fine. So what i'm doing wrong or im missing something.
The Api Key where ok and they are working.
my login implementaion is like:
private ImageButton loginButton;
private LoginButton fbbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
}
#Override
protected void onResume() {
super.onResume();
callbackManager= CallbackManager.Factory.create();
fbbtn= (LoginButton)findViewById(R.id.fb_button);
fbbtn.setReadPermissions("public_profile", "email","user_friends");
loginButton = (ImageButton) findViewById(R.id.login_btn);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
fbbtn.performClick();
fbbtn.setPressed(true);
fbbtn.invalidate();
fbbtn.registerCallback(callbackManager, mCallBack);
fbbtn.setPressed(false);
fbbtn.invalidate();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
progressDialog.dismiss();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
app.setUser(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
progressDialog.dismiss();
}
#Override
public void onError(FacebookException e) {
progressDialog.dismiss();
}
};
To change user profile you need logged out from it in your FB app (or in Chrome if you are using Chrome Custom Tabs)
I used the following code snippet in my Android app to ask user to login to Facebook
public class FacebookLoginActivity extends AppCompatActivity {
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
initCallback();
LoginManager.getInstance().logInWithReadPermissions(this, FacebookUtils.requiredPermissions);
}
private void initCallback() {
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken.setCurrentAccessToken(loginResult.getAccessToken());
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException exception) {
}
}
);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
However, the methods defined in the callback never gets called. Anyone can help me?
I am sharing a video on FB via share dialog in Android. The sharing works perfectly fine. However, FB post id returns null. The callback returns even before the video is uploaded. Please let me know, if I missing something. Below is my code.
public class TestFragment extends Fragment {
private CallbackManager callbackManager;
private ShareDialog shareDialog;
public TestFragment() {
// Required empty public constructor
}
public static TestFragment newInstance(String path, String json) {
TestFragment fragment = new TestFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity());
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
// this part is optional
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Timber.d("result.getPostId() :: " + result.getPostId());
}
#Override
public void onCancel() {
Timber.d("Facebook : Cancelled");
}
#Override
public void onError(FacebookException e) {
Timber.d(e.getMessage());
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, container, false);
ButterKnife.inject(this, view);
return view;
}
#OnClick(R.id.facebookShare)
public void share() {
Timber.d("share button pressed");
if (ShareDialog.canShow(ShareVideoContent.class)) {
Timber.d("showing share dialog");
shareDialog.show(getVideoContent());
} else {
Timber.d("unable to show the share dialog");
}
}
private ShareVideoContent getVideoContent() {
Timber.d(mVideoMetadata.getVideoId());
ShareVideo shareVideo = new ShareVideo.Builder()
.setLocalUrl(Uri.parse("... file ..."))
.build();
ShareVideoContent content = new ShareVideoContent.Builder()
.setVideo(shareVideo)
.build();
return content;
}
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
shareDialog.show(shareContent, ShareDialog.Mode.FEED);
Set mode to ShareDialog.Mode.FEED.
It's working for me .
Here's example
When Login Success With Facebook Then Call This Method
shareOnFacebook()
private void shareOnFacebook(){
ShareLinkContent shareContent = new ShareLinkContent.Builder()
.setContentTitle("The Simpson!")
.setContentUrl(Uri.parse("http://www.codecube.in/airbucks-project"))
.build();
mFacebookShareDialog.show(shareContent, ShareDialog.Mode.FEED);
mFacebookShareDialog.registerCallback( this.callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.v("MyApp", "Share success!"); //Showed if I press the share or the cancel button
String postID = result.getPostId();
Log.v("MyApp", "Share success!" +result.getPostId());
}
#Override
public void onCancel() {
Log.v("MyApp", "Share canceled"); //Only showed when I press the close button
}
#Override
public void onError(FacebookException e) {
Log.v("MyApp","Share error: " + e.toString());
}
});
mFacebookShareDialog.show(shareContent, ShareDialog.Mode.FEED);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}