Posting programatically on facebook using facebook sdk for android - android

I tried to post on my wall a message by clicking on a button in my activity. I first logged in and i am able to get some of my personal information from facebook too. But, when i am trying to post something on wall. It is not working.
public void postOnWall(String msg) {
Log.d("Tests", "Testing graph API wall post");
try {
String response = mFacebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("message", "Test Message");
parameters.putString("description", "test test test");
response = mFacebook.request("me/feed", parameters,
"POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}
This is the function i am trying to post on to my wall.
Can anybody help me out.
Response from facebook:
{"error":{"message":"An active access token must be used to query information about the current user","type":"OAuthException","Code":2500}}
Code to login:
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}

Related

How to update status on facebook

I want to update status on facebook by app ,so i have searched a lot but not able to update status on facebook ,i have imported facebook sdk3.15.0,it gives permission error in logcat.so let me know how is it possible to update status ,and it gives response null ,my code is
imgfacebook.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
loginToFacebook();
}
});
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
postToWall(CheckRating);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
postToWall(CheckRating);
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
public void postToWall(final String msg) {
try {
// Bundle parameters = new Bundle();
// parameters.putString("message", "Text is lame. Listen up:");
// parameters.putString("name", "Name");
// parameters.putString("link", "http://www.google.com");
// parameters.putString("caption", "Caption");
// parameters.putString("description", "Description");
//
// String response = facebook.request("me/feed", parameters,
// "POST");
// Log.v("response", response);
Bundle parameters = new Bundle();
parameters.putString("message", "Text is lame. Listen up:");
parameters.putString("method", "stream.publish");
String response = facebook.request(parameters);
Log.v("response", response);
System.out.println("Response is ....." + response);
} catch (Exception e) {
System.out.println("Response is nulll......");
}
// post on user's wall.
}

how to use recent facebook SDK in android?

I am developing an app which can log in facebook and write post on my wall ..
I am using this tutorial : http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/
it's working fine but eclipse tell me all of this methods are deprecated ..
Now how can i replace it with recent version ??
Thanks in advance :)
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
} else {
postOnWall(quote);
}
}
// ==============================================================================
public void postToWall() {
// post on user's wall.
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onComplete(Bundle values) {
}
#Override
public void onCancel() {
}
});
}
// ==============================================================================
public void postOnWall(String msg) {
Log.d("Tests", "Testing graph API wall post");
try {
String response = facebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("message", msg);
parameters.putString("description", "test test test");
response = facebook.request("me/feed", parameters,
"POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}

Facebook sdk: publish url to "what's on your mind box" using params caption android

I am posting a url to the "what in your mind box" of facebook using facebook sdk but when i check on facebook account, nothing is published. Can someone tell me how to use caption to publish url to facebook wall using facebook sdk please.
/**
* Function to login into facebook
* */
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
Constants.facebook.setAccessToken(access_token);
/*
btnFbLogin.setVisibility(View.INVISIBLE);
// Making get profile button visible
btnFbGetProfile.setVisibility(View.VISIBLE);
// Making post to wall visible
btnPostToWall.setVisibility(View.VISIBLE);
// Making show access tokens button visible
btnShowAccessTokens.setVisibility(View.VISIBLE);
*/
Log.d("FB Sessions", "" + Constants.facebook.isSessionValid());
}
if (expires != 0) {
Constants.facebook.setAccessExpires(expires);
}
if (!Constants.facebook.isSessionValid()) {
Constants.facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
Constants.facebook.getAccessToken());
editor.putLong("access_expires",
Constants.facebook.getAccessExpires());
editor.commit();
// Making Login button invisible
/*btnFbLogin.setVisibility(View.INVISIBLE);
// Making logout Button visible
btnFbGetProfile.setVisibility(View.VISIBLE);
// Making post to wall visible
btnPostToWall.setVisibility(View.VISIBLE);
// Making show access tokens button visible
btnShowAccessTokens.setVisibility(View.VISIBLE);
*/}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
postonWall("http://google.com/");
public void postOnWall(String msg) {
try {
String response = facebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("caption",url);
// parameters.putString("caption", "Test Caption: http://google.com/");
// parameters.putString("method", "stream.publish");
response = facebook.request("me/feed", parameters,
"POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}
i check your code, make sure your session is valid.
exception throws: "Missing message or attachment".
So, please pass "message" also with request like below.
public void postOnWall(String msg) {
try {
String response = facebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("caption",msg);
parameters.putString("message","message1");
response = facebook.request("me/feed", parameters,
"POST");
Log.d("System out", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.e("System out", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
}
see more parameters
hope it help u:)

How can i get user id's who are login with facebook credentials in to my android application

In my application I am using login with facebook, if user clicks on button it shows facebook login screen, if user entered the credentials I would like to get the user id, here I am pasting login() code could you please tell me what are the changes I have to do for getting the user id?
private void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
try {
editor.putString("access_expires",
facebook.request("me"));
try {
JSONObject json = Util.parseJson(facebook
.request("me", values));
String userId = json.getString("id");
} catch (FacebookError e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Log.i(TAG, facebook.request("me"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
editor.commit();
}
#Override
public void onError(DialogError error) {
// Function to handle error
Log.wtf(TAG, error);
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
Log.wtf(TAG, fberror);
}
});
}
}
There's an api call for user info of the current user, which will include their id:
https://graph.facebook.com/me
You can hit that url by calling:
facebook.request("me")
after authenticating.
It returns a json user object, which you can parse and get the id from:
https://developers.facebook.com/docs/reference/api/user/

Android: Facebook SDK - Share Post with description, thumbnail and title

I'm having trouble setting up a simple facebook wall post to the user's wall.
I want a facebook dialog box to pop up on clicking a button with a thumbnail, description and title.
I have tried the following code but no dialog box pops up:
shareOnFacebookBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
/*
* Get existing access_token if any
*/
mPrefs ShopDetailActivity.this.getActivity().getPreferences(Context.MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null) {
facebook.setAccessToken(access_token);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {
facebook.authorize(ShopDetailActivity.this.getActivity(),new String[] { "publish_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
//facebook.dialog(ShopDetailActivity.this.getActivity(), "feed", new SampleDialogListener());
Bundle parameters = new Bundle();
parameters.putString("message", "message");// the message to post to the wall
facebook.dialog(context, "feed", parameters, this);
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
}
});
The authorize window opens and after clicking allow I'd expect the dialog box to pop up but it just returns to the app.
What am I doing wrong?
facebook = new Facebook("your facebook id");
mAsyncRunner = new AsyncFacebookRunner(facebook);
facebook.authorize(this, new String[]
{ "publish_stream", "offline_access" }, -1,
new DialogListener()
{
public void onComplete(Bundle values)
{
Log.e("tag", "Values returned by Bundle ====> " + values.toString());
fbImageSubmit();
}
public void onFacebookError(FacebookError error)
{
}
public void onError(DialogError e)
{
}
public void onCancel()
{
}
});
//add method into your class
private void fbImageSubmit()
{
if (fb != null)
{
if (fb.isSessionValid())
{
Bundle b = new Bundle();
b.putString("picture", your image url);
b.putString("caption", title);
b
.putString(
"description",
"test");
b.putString("name", "Hi Friends, I am using the your app name app for Android!");
b.putString("link", "https://market.android.com/details?id="+this.getApplication().getPackageName().toString());
try
{
String strRet = "";
strRet = fb.request("/me/feed", b, "POST");
JSONObject json;
try
{
json = Util.parseJson(strRet);
if (!json.isNull("id"))
{
Log.i("Facebook", "Image link submitted.");
}
else
{
Log.e("Facebook", "Error: " + strRet);
}
} catch (FacebookError e)
{
Log.e("Facebook", "Error: " + e.getMessage());
}
} catch (Exception e)
{
Log.e("Facebook", "Error: " + e.getMessage());
}
}
}
}

Categories

Resources