android facebook api post - android

I have a problem. I want to use the facebook api and make a post to my wall without calling a dialog. Basically I have an app and I want people to be able to share the app, so I want to have a specific message to post. I keep getting a response of "Method not implemented". Heres the code for the post.
//I tried this also ->>String path = "http://graph.facebook.com/me/feed";
String path = "https://api.facebook.com/method/stream.publish";
Bundle b = new Bundle();
//And i tried this -> b.putString("access_token",facebook.getAccessToken());
b.putString("message", "this is just a test...");
try {
String ret = facebook.request(path, b);
Toast.makeText(fmasterActivity.this, ret, Toast.LENGTH_LONG).show();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

I am assuming that you are doing that bit of code after the user successfully authenticates?
This bit of code worked for me:
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
private void onFacebookShare() {
mFacebook = new Facebook();
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionEvents.addAuthListener(new SampleAuthListener());
SessionEvents.addLogoutListener(new SampleLogoutListener());
}
private void postToFBWall() {
if(mFacebook.isSessionValid()){
shareVideoOnFB();
} else {
showDialog(DIALOG_FBOOK_LOGIN);
}
}
public void shareVideoOnFB(){
Bundle params = new Bundle();
params.putString("message", "This string will appear as the status message");
params.putString("link", "This is the URL to go to");
params.putString("name", "This will appear beside the picture");
params.putString("caption", "This will appear under the title");
params.putString("description", "This will appear under the caption");
params.putString("picture", "This is the image to appear in the post");
mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {
public void onMalformedURLException(MalformedURLException e) {}
public void onIOException(IOException e) {}
public void onFileNotFoundException(FileNotFoundException e) {}
public void onFacebookError(FacebookError e) {}
public void onComplete(String response) {
logoutFacebook();
}
});
Toast.makeText(ShareActivity.this, "Posting to your Wall...", Toast.LENGTH_SHORT).show();
}
You can call onFacebookShare() in your activity's onCreate(), and then when the user presses whatever to indicate that s/he wants to share on Facebook, call postToFBWall(). Of course you have to add in handling to show the login dialog.

Related

how to send invitation on Facebook in Android?

I am trying to send invitation from my app to facebook friends. everything is working fine, facebook invitation dialog is being shown, my frinds list is there, I am also receiving status in onComplete but on the other side (to whom I send invitation) nothing happens. not notification is being sent to my frinds. please let me know where I am wrong. below is my code.
private void inviteFriends() {
try {
Bundle params = new Bundle();
params.putString("title", "invite friends");
params.putString("message", "come join us!");
mFacebook.dialog(this, "apprequests", params, new DialogListener() {
#Override
public void onComplete(Bundle values) {
try {
// JSONObject eventResponse = new JSONObject(response);
// event_id = event.getString("id");
/*
* Log.i(TAG, "Event Response => "+eventResponse);
* Log.w("myapp", friends);
*/
System.out.println("fb invite response "
+ values.toString());
Toast.makeText(getApplicationContext(), "Request sent",
Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(),
// "New Event Created!!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
}
}
#Override
public void onFacebookError(FacebookError error) {
}
#Override
public void onCancel() {
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
}
}
now problem is that request is sent but notification comes in facebook app not in facebook browser?
You should use the Message Dialog to invite friends in an Android App, the App Requests are only for Canvas Games: https://developers.facebook.com/docs/apps/faq#invite_to_app
(See "If your app does not have a presence on Facebook Canvas")
More information: https://developers.facebook.com/docs/android/share#message-dialog

Android facebook feed posting error

I'm trying to post on facebook from my android app. It is working fine using previously created app id.
Some thread is saying it is because of publish_actions.
But using new on it is giving following error.
Response: {"error":{"message":"(#200) Requires extended permission: publish_actions","type":"OAuthException","code":200}}
My posting code is below:
public void shareVideoInFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
byte[] data = null;
// shareAdvertisement();
String link = "http://techslides.com/demos/sample-videos/small.mp4";
String dataMsg = "Share for DateClip";
Bundle param;
facebook = new Facebook(APP_ID);
System.out.println("Access Token: " + pref.getFbAccessToken());
facebook.setAccessToken(pref.getFbAccessToken());
facebook.setAccessExpires(pref.getFbAccessExpire());
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
InputStream is = null;
try {
// is = new FileInputStream(dataPath);
// data = readBytes(is);
param = new Bundle();
param.putString("message", dataMsg);
param.putString("filename", "DateClip");
// param.putByteArray("video", data);
param.putString("name", "DateClip");
param.putString("caption",
"Build great social apps and get more installs.");
param.putString("description", "Watch the Video");
param.putString("link", link);
// param.putString("picture",
// "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
mAsyncRunner.request("me/feed", param, "POST",
new RequestListener() {
#Override
public void onMalformedURLException(
MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
System.out.println("IO Exception.........");
}
#Override
public void onFileNotFoundException(
FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(),
// "File not found...", Toast.LENGTH_LONG).show();
System.out.println("FileNotfound.........");
}
#Override
public void onFacebookError(FacebookError e,
Object state) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(),
// "Error...", Toast.LENGTH_LONG).show();
System.out.println("Error.........");
}
#Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(),
// "Sharing Completed...",
// Toast.LENGTH_LONG).show();
System.out
.println("Successfully Sent.......................................................");
System.out.println("Response: " + response);
runOnUiThread(new Runnable() {
public void run() {
// UI code goes here
Toast.makeText(getApplicationContext(),
"Successfully shared on Facebook.",
Toast.LENGTH_LONG).show();
// shareForPassionPoint();
}
});
}
}, null);
// mAsyncRunner.request(dataPath, parameters, httpMethod, new F,
// state);
} catch (Exception e) {
e.printStackTrace();
}
}
Please help me.
This was the case with out iOS app. It may be the same for Android also.
"
Please get your app reviewed again.
Recently, all the permissions except 3 basic ones (email, public_profile and user_friends) have been removed from the app if you had it reviewed by an older version of the Graph API.
Follow the steps as described here: https://developers.facebook.com/docs/facebook-login/review
"
Hope this helps :)

Android Facebook API not sending APP Requests

I am using the Android Hackbook app. I have slightly modified the code to send a app request to a a Custom Friend. Here is the code.
Bundle params = new Bundle();
params.putString("title", "invite friends");
params.putString("message", getString(R.string.request_message));
params.putString("to", userId);
Utility.mFacebook.dialog(Hackbook.this, "apprequests", params, new AppRequestsListener());
This brings up a dialog to send a app request to the friend who user id i selected. The AppRequestsListener code gets executed and it shows me a toast that the request was successful. How the user never receives the request. What am i doing wrong. Is there some permission that i need to grant to the application first. Really stuck, any help would be good
Kind Regards,
Try this:
Facebook facebook = new Facebook(YOUR_APP_ID);
Bundle params = new Bundle();
params.putString("to", userId);
params.putString("message", "getString(R.string.request_message)");
facebook.dialog(((Activity) ctx).getParent(), "apprequests",
params, new DialogListener() {
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Toast.makeText(ctx, "<<Complete>>",
Toast.LENGTH_SHORT).show();
}
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
public void onCancel() {
// TODO Auto-generated method stub
}
});
This works for me. User receive request as expected.

NPE when post image along with text caption via facebook SDK

I want to post image along with text caption via facebook SDK.
I am referring to the solution posted here.
Android post picture to Facebook wall
However, I am getting the following error.
FATAL EXCEPTION: main
java.lang.NullPointerException
at java.net.URLDecoder.decode(URLDecoder.java:104)
at java.net.URLDecoder.decode(URLDecoder.java:48)
at com.facebook.android.Util.openUrl(Util.java:173)
at com.facebook.android.Facebook.request(Facebook.java:751)
at com.xxx.yyy.gui.share.ShareDialogFactory.shareThroughFacebook(ShareDialogFactory.java:170)
I had verified my code against the previous posted solution.
private static void shareThroughFacebook(final Context context, final File file, final int heartbeatForShare) {
final Facebook facebook = new Facebook("285243934881501");
// Login.
facebook.authorize((Activity)context, new String[] { "publish_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
android.util.Log.i("CHEOK", "onComplete");
}
#Override
public void onFacebookError(FacebookError error) {
android.util.Log.i("CHEOK", "onFacebookError");
}
#Override
public void onError(DialogError e) {
android.util.Log.i("CHEOK", "onError");
}
#Override
public void onCancel() {
android.util.Log.i("CHEOK", "onCancel");
}
});
android.util.Log.i("CHEOK", "STEP 1");
final byte[] data = JPEG2ByteArray(file);
final Bundle bundle = new Bundle();
bundle.putString(Facebook.TOKEN, facebook.getAccessToken());
android.util.Log.i("CHEOK", "data length is " + data.length);
bundle.putByteArray("picture", data);
bundle.putString("message", "this-is-message");
bundle.putString("caption", "this-is-caption");
// I can reach till here!
android.util.Log.i("CHEOK", "STEP 2");
try {
// NPE happens right here!!!
facebook.request("me/photos", bundle, "POST");
} catch (FileNotFoundException e1) {
android.util.Log.i("CHEOK", "FileNotFoundException");
e1.printStackTrace();
} catch (MalformedURLException e1) {
android.util.Log.i("CHEOK", "MalformedURLException");
e1.printStackTrace();
} catch (IOException e1) {
android.util.Log.i("CHEOK", "IOException");
e1.printStackTrace();
}
android.util.Log.i("CHEOK", "STEP 3");
}
I can confirm my byte array is non-zero length. I do not receive any callback during authorize, not sure whether this is correct behaviour. Note, the code shareThroughFacebook is invoked from a Dialog.
Is there any other steps I had missed out?

Facebook wall post from multiple Activities

I have been trying to make wall post from multiple pages but when i try to make post from other pages except original facebook page, it is giving me an exception of java.lang.NullPointerException
The method i am calling and created in a Facebook page.
public void setConnection() {
mContext = this;
mFacebook = new Facebook(getResources().getString(R.string.FACEBOOK_ID_TEST));
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
}
public boolean isSession() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String access_token = sharedPrefs.getString("access_token", "x");
Long expires = sharedPrefs.getLong("access_expires", -1);
Log.d(TAG, access_token);
if (access_token != null && expires != -1) {
mFacebook.setAccessToken(access_token);
mFacebook.setAccessExpires(expires);
}
return mFacebook.isSessionValid();
}
public void getID()
{
Bundle bundle = new Bundle();
bundle.putString("fields", "birthday");
try {
mFacebook.request("me/friends", bundle);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Error in FaceBook Friends List","Exception = "+e.getMessage());
e.printStackTrace();
}
if (isSession()) {
Log.d(TAG, "sessionValid");
try {
mFacebook.request("me/friends", bundle);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Error in FaceBook Friends List","Exception = "+e.getMessage());
e.printStackTrace();
}
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMISSIONS, new LoginDialogListener());
}
}
I am able to get the wall post dialog box but its showing "An error occurred. Please try again later".
Please help me out.
Thanks in advance.
I think the best way for you to achieve this would be to make an abstract class which extends the super class Activity, use this to hold all your facebook functions.
Then when you want to have an activity which has the facebook functionality then you would just extend this abstract class. If your not sure what I mean then take a look at the tutorial here, it goes through how to setup a basic Facebook Activity class. Hope this helps.

Categories

Resources