I am working on facebook android application but there is one problem i am facing
i am using the following example
Android/Java -- Post simple text to Facebook wall?
so the problem is that everything works here fine, the dialogs etc etc but When it open the screen to upload Walla Message that i have setted up here
try
{
System.out.println("*** IN TRY ** ");
Bundle parameters = new Bundle();
parameters.putString("message", "this is a test");// the message to post to the wall
facebookClient.dialog(this, "stream.publish", parameters, this);// "stream.publish" is an API call
}
catch (Exception e)
{
// TODO: handle exception
System.out.println(e.getMessage());
}
It does not show me my Message written in the dialog. Whats the problem with that
can anybody Guide me please..
Thanks alot.
Message has been ignored. You can read about it here: https://developers.facebook.com/docs/reference/dialogs/feed/
This field will be ignored on July 12, 2011 The message to prefill the text field that the user will type in. To be compliant with Facebook Platform Policies, your application may only set this field if the user manually generated the content earlier in the workflow. Most applications should not set this.
Use this code , it working for me :
Bundle parameters = new Bundle();
parameters.putString("message",sharetext.getText().toString());// the message to post to the wall
//facebookClient.dialog(Jams.this, "stream.publish", parameters, this);// "stream.publish" is an API call
facebookClient.request("me/feed", parameters, "POST");
Hope it helps..
Edited:
public class FacebookActivity implements DialogListener
{
private Facebook facebookClient;
private LinearLayout facebookButton;
public FacebookActivity(Context context) {
facebookClient = new Facebook();
// replace APP_API_ID with your own
facebookClient.authorize(Jams.this, APP_API_ID,
new String[] {"publish_stream", "read_stream", "offline_access"}, this);
}
#Override
public void onComplete(Bundle values)
{
if (values.isEmpty())
{
//"skip" clicked ?
}
// if facebookClient.authorize(...) was successful, this runs
// this also runs after successful post
// after posting, "post_id" is added to the values bundle
// I use that to differentiate between a call from
// faceBook.authorize(...) and a call from a successful post
// is there a better way of doing this?
if (!values.containsKey("post_id"))
{
try
{
Bundle parameters = new Bundle();
parameters.putString("message",sharetext.getText().toString());// the
message to post to the wall
//facebookClient.dialog(Jams.this, "stream.publish", parameters,
this);// "stream.publish" is an API call
facebookClient.request("me/feed", parameters, "POST");
sharetext.setText("");
Toast.makeText(Jams.this,"Message posted
successfully.",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
// TODO: handle exception
// System.out.println(e.getMessage());
}
}
}
#Override
public void onError(DialogError e)
{
return;
}
#Override
public void onFacebookError(FacebookError e)
{
return;
}
#Override
public void onCancel()
{
return;
}
}
Related
I am getting this error message while trying to open the facebook page of the user.The strange thing is that if I have a mutual friend with that user the page loads with no problem, but I don't think it's default behavior, otherwise I can't understand the meaning of user_link permission.
Facebook has approved user_link permission and I passed App Review.
From developer account I changed the API version the app calls to v3.1.
The way I am getting user_link
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_gender", "user_link"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
makeGraphRequest(loginResult.getAccessToken());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
public void makeGraphRequest(AccessToken token) {
GraphRequest request = GraphRequest.newMeRequest(
token, (object, response) -> {
try {
userStorage.setUserProfileUrl(object.getString(AppConstants.LINK));
} catch (JSONException e) {
e.printStackTrace();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "name,gender,link,picture.width(700).height(700)");
request.setParameters(parameters);
request.executeAsync();
}
And using this answer for opening the page.
Intent intent;
try {
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" + currentUser.getLink()));
context.startActivity(intent);
} catch (Exception e) {
intent = new Intent(context, FacebookWebViewActivity.class);
intent.putExtra(AppConstants.USER_LINK, currentUser.getLink());
context.startActivity(intent);
}
build.gradle
implementation 'com.facebook.android:facebook-login:4.33.0'
I will be very thankful if someone can suggest any solution. I faced this issue before the facebook new API version also.
From this post we can find the following bit of text
For most apps, links will only redirect to the individual's Facebook
profile for logged in people in the person's extended network.
So, I guess there is no way to be 100% sure that the user page will be loaded properly.
https://developers.facebook.com/docs/graph-api/changelog/version3.0#login
...the following fields that belonged to public_profile are deprecated...
As you can read in the changelog, the "link" and "gender" fields are deprecated.
Edit: But they have introduced new permissions user_link and user_gender so that users can explicitly asked for those particular fields. (Thanks to CBroe for pointing that out)
I am getting this error message while trying to open the facebook page of the user.The strange thing is that if I have a mutual friend with that user the page loads with no problem, but I don't think it's default behavior, otherwise I can't understand the meaning of user_link permission.
Facebook has approved user_link permission and I passed App Review.
From developer account I changed the API version the app calls to v3.1.
The way I am getting user_link
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_gender", "user_link"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
makeGraphRequest(loginResult.getAccessToken());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
public void makeGraphRequest(AccessToken token) {
GraphRequest request = GraphRequest.newMeRequest(
token, (object, response) -> {
try {
userStorage.setUserProfileUrl(object.getString(AppConstants.LINK));
} catch (JSONException e) {
e.printStackTrace();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "name,gender,link,picture.width(700).height(700)");
request.setParameters(parameters);
request.executeAsync();
}
And using this answer for opening the page.
Intent intent;
try {
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" + currentUser.getLink()));
context.startActivity(intent);
} catch (Exception e) {
intent = new Intent(context, FacebookWebViewActivity.class);
intent.putExtra(AppConstants.USER_LINK, currentUser.getLink());
context.startActivity(intent);
}
build.gradle
implementation 'com.facebook.android:facebook-login:4.33.0'
I will be very thankful if someone can suggest any solution. I faced this issue before the facebook new API version also.
From this post we can find the following bit of text
For most apps, links will only redirect to the individual's Facebook
profile for logged in people in the person's extended network.
So, I guess there is no way to be 100% sure that the user page will be loaded properly.
https://developers.facebook.com/docs/graph-api/changelog/version3.0#login
...the following fields that belonged to public_profile are deprecated...
As you can read in the changelog, the "link" and "gender" fields are deprecated.
Edit: But they have introduced new permissions user_link and user_gender so that users can explicitly asked for those particular fields. (Thanks to CBroe for pointing that out)
Hi I am new to android programming. I want to share a image with some description on facebook.
I have tried every method explain in answers on stackoverflow. My problem is facebook dialog opens but it doesn't have specified bundle parameters.
Please help me with this. I have already tried almost 20 different code snippets. Please give me fully functional code.
#Override
public void onClick(View v) {
fb = new Facebook(app_id);
Bundle par = new Bundle();
par.putString("name", "Ass");
fb.dialog(con,"feed",par, new DialogListener(){
#Override
public void onCancel() {
// TODO Auto-generated method stub
}
#Override
public void onComplete(Bundle arg0) {
// TODO Auto-generated method stub
}
#Override
public void onError(DialogError arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFacebookError(FacebookError arg0) {
// TODO Auto-generated method stub
}});
}
});
}
Have you already set up the facebook side of the application on the development page?
You need to have your app registered on facebook if you are intending to use their api and/or do graph requests or wall posts on behalf of a user authtoken.
You should read this tutorial (check part 5 for fb app registering) and all of the related info around it to really get going with facebook interaction within your app.
I know I did and end up creating my own library for log-in short-cuts or graph requests etc... it's not that simple, it will take you some time aswell.
Also;
Are you using the loginButton from the sdk? Like this:
<com.facebook.widget.LoginButton
android:id="#+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
It automatically handles user login to facebook whether he has the facebook app installed or not and retrieves a session status result (onStatusChange callback).
Make sure you handle that right first and that the session is correctly initiated.
Still, posting your log will give us a better idea of what you encountering with.
I have to say, tho, that the facebook api for android is pretty solid so far so you must be doing something wrong for sure.
<<<<<< EDIT: >>>>>>
Ok then, assuming you have the user correctly logged-in (session.getActiveSession() == Session.OPENED I believe), the next step is to make sure you enabled necessary permissions.
This example is from the official facebook documentation, try it (execute publishStory() in your app):
private void publishStory() {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", "Facebook SDK for Android");
postParams.putString("caption", "Build great social apps and get more installs.");
postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
postParams.putString("link", "https://developers.facebook.com/android");
postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(getActivity()
.getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity()
.getApplicationContext(),
postId,
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
The code issues a POST to the users feed wall with the graph params specified at the bundle.
If session does not have the needed publish permissions, then a permission request will be issued instead. If user granted those permissions, then the RequestAsyncTask should be executed next time you call the method.
As you can see the basic idea is to get the user to log-in with his facebook account (first step), then request necessary permissions for the action, in this case, publish permissions for a wall post (second step), and lastly issue a graph request into "me/feed" with the params needed for the wall post.
In any case, if you still encountering problems please try to debug from your log as it indicates wether the request failed because of invalid session or no permissions etc...
Post your log here in that case , but this should work.
In my application, I'm manually setting an access token that's stored server-side. When I try and use an FB.dialog to show an invite dialog, the web view displays "An error occured with . Please try again later. API Error Code: 110 API Error Description: Invalid user id Error message: Missing user cookie (to validate session user)
I've verified that the token is valid, and I've been able to make requests requests with it.
Here's the implementation:
Facebook facebook = new Facebook("my app id");
try {
facebook.setAccessToken(authToken);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(tokenExpiration);
facebook.setAccessExpires(date.getTime());
Bundle params = new Bundle();
params.putString("message", "Invite Friends!");
facebook.dialog(mActivity, "apprequests", params, new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
Log.e(e.getMessage(), e);
}
#Override
public void onError(DialogError e) {
Log.e(e.getMessage(), e);
}
#Override
public void onComplete(Bundle values) {
}
#Override
public void onCancel() {
}
}); .....
No callback is made for onFacebookError or onError
Figured this out, it was related to a partner's SDK messing w/ cookies.
Hi I am trying to add facebook connect to my Android app , I did managed to post on my wall with app but when I downloaded android facebook app and logged in on it but now I cant post on my wall. I am getting this error:
{"error":{"type":"OAuthException","message":"An active access token
must be used to query information about the current user."}}
CODE:
public class FacebookLogin extends Activity {
private static final String APP_API_ID = "080808998-myappId";
Facebook facebook = new Facebook(APP_API_ID);
private AsyncFacebookRunner mAsyncRunner;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAsyncRunner = new AsyncFacebookRunner(facebook);
SessionStore.restore(facebook, this);
SessionEvents.addAuthListener(new SampleAuthListener());
Log.i("Error", facebook.getAccessToken()+"tokenId");
facebook.dialog(FacebookLogin.this, "feed", new SampleDialogListener());
}
public void postOnWall(String msg) {
try {
Bundle bundle = new Bundle();
bundle.putString("message", msg);
bundle.putString("from", "fromMe");
bundle.putString("link","facebook.com");
bundle.putString("name","name of the link facebook");
bundle.putString("description","some description here");
//bundle.putString(Facebook.TOKEN, accessToken);
bundle.putString("picture", "http://url to image");
String response = facebook.request("me/feed",bundle,"POST");
Log.d("Error", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
THe token I am getting is null.
You didn't check if the session is valid or not.
If not, then you have to authorize.
You also have to sign in to Facebook the first time to use your app and give the permissions to the app.
Then you will get the access token for the first time.
Then you should store your session and when using the app again you will not get this error.
To do so, please review the Example application LoginButton.java init() method from Facebook_Android_SDK
Note: make a boolean flag stored in your sharedPreferences to indicate if this is the first time or not,
so the login dialog doesn't pop up every time you use the application.