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.
Related
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
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 :)
I have a problem with inviting firend by facebook api to my app. This is my method:
private void inviteFromFacebook(Activity activity, List<GraphUser> list) {
for(int i=0; i<list.size(); i++) {
//TODO post invite to friends wall
// Log.v("MainActivity", "user id: " + user.getId());
if(list == null || list.size() == 0)
return;
Bundle parameters = new Bundle();
String friendsIdsInFormat = "";
friendsIdsInFormat = friendsIdsInFormat + list.get(i).getId();
parameters.putString("to", friendsIdsInFormat);
parameters.putString( "message", "Use my app!");
Facebook mFacebook = new Facebook( getResources().getString(R.string.facebook_app_id));
// Show dialog for invitation
mFacebook.dialog(activity, "apprequests", parameters, new Facebook.DialogListener() {
#Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
}
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
});
}
}
Everyting is ok, but I want to send invitation to many frieds, so in this codes I show one dialog for every person. How can I show only one dialog, but with many friends? This is possible or I must for every friend show dialog?
You can send a comma delimited list in the "to" field, like "123,456,789".
Be careful about sending too many requests though, you don't want to be identified as a spam app.
I'm trying to develop app that can post to Facebook.
So SDK is set up, I can log in using FB button like in hackbook example.
How ever I do not really like the way it looks. I'm developing an app and then floating webview pop's up on top of it?
Maybe I'm getting it all wrong, but I'd love to get Facebook login / password somehow within my settings activity, and then save token to shared preferences.
Is there a way to do it?
Saving the access tokens in SharedPrefs can be done by using the following code when you are setting up a valid session.
Utility.fb.authorize(MainActivity.this,Utility.fbPermissions , new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(context, "Facebook Error", Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
Toast.makeText(context, "Dialog Error", Toast.LENGTH_LONG).show();
}
#Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Toast.makeText(context, "User Logged In", Toast.LENGTH_LONG).show();
Utility.editor = Utility.prefs.edit();
Utility.editor.putString("access_token", Utility.fb.getAccessToken());
Utility.editor.putLong("access_expires", Utility.fb.getAccessExpires());
Utility.editor.commit();
Intent intent = new Intent(context, UpdateStatus.class);
startActivity(intent);
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(context, "Cancelled by user", Toast.LENGTH_LONG).show();
}
});
And later you can just check for the validity of the access token and log in the user directly, without asking for his username or password.
Utility.prefs = getSharedPreferences(Utility.PREF_UTILITY_FILE_NAME, MODE_PRIVATE);
Utility.access_token = Utility.prefs.getString("access_token", null);
Utility.expires = Utility.prefs.getLong("access_expires", 0);
if(Utility.access_token != null)
{
Utility.fb.setAccessToken(Utility.access_token);
}
if(Utility.expires !=0)
{
Utility.fb.setAccessExpires(Utility.expires);
}
if(Utility.fb.isSessionValid())
{
//user is already logged in
}
EDIT : Ever since Facebook API removed the offline access permission, you can increase the validity of the access tokens by putting the following code in the activity where you are doing the authentication to authorize the user login.
public void onResume() {
super.onResume();
Utility.fb.extendAccessTokenIfNeeded(this, null);
}
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.