How to update status on facebook - android

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.
}

Related

Getting user facebook likes and attributes in Android(Facebook SDK 3.18)

This is my login function:-
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
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
}
});
}
}
This my function to get facebook likes and attributes:
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
id=profile.getString("id");
// getting name of the user
name = profile.getString("name");
// getting email of the user
email = profile.getString("email");
//about=profile.getString(about);
bio=profile.getString(bio);
birthday=profile.getString(birthday);
gender=profile.getString(gender);
link=profile.getString(link);
locale=profile.getString(locale);
political=profile.getString(political);
quotes=profile.getString(quotes);
relationship_status=profile.getString(relationship_status);
relegion=profile.getString(relegion);
website=profile.getString(website);
Log.d("Name",name);
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
});
//Getting Facebook Like
Session session = Session.getActiveSession();
new Request(
session,
"/{id}/likes",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
Log.d("Facebook Likes", response.toString());
}
}
).executeAsync();
/*Session session = Session.getActiveSession();
Request.Callback callback = new Request.Callback() {
#Override
public void onCompleted(Response response) {
// response should have the likes
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
Log.d("Facebook Likes", response.toString());
}
};
Request request = new Request(session, "me/likes", null, HttpMethod.GET, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();*/
ERROR:-
D/Request﹕ Warning: Sessionless Request needs token but missing either application ID or client token.
I am getting other attributes as
28959-29486/com.example.sensetest D/Profile﹕ {"id":"************","email":"***********","first_name":"***","gender":"male","last_name":"****","link":"****","locale":"en_US","middle_name":"Singh","name":"Rituraj Singh Rathore","timezone":5.5,"updated_time":"2015-03-22T11:01:08+0000","verified":true}
The problem is I am not able to get facebook likes of user who has logged in. I have tried also How to get user likes from Facebook with Android sdk 3.0.
But I didn't got desired result.
Help me regarding this, Where am i doing wrong. What should i do to make it right?

Posting programatically on facebook using facebook sdk for 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
}
});
}
}

Unable to use facebook login in android device

I have same issue mentioned in this stack overflow question. But couldn't find the solution. My problem is when I use facebook login in my android device(which is not installed facebook app), my app works as expected. If I install the facebook application my code doesn't work anymore and It throws the error "Login failed. Please contact the maker of this app and ask them to report issue #1118578 to Facebook". My code is posted below
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAsyncRunner = new AsyncFacebookRunner(facebook);
loginToFacebook();
}
#SuppressWarnings("deprecation")
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
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();
getProfileInformation();
} }); } }
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
/**
* Get Profile information by making request to Facebook Graph API
* */
#SuppressWarnings("deprecation")
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
// Facebook Profile JSON data
JSONObject profile = new JSONObject(json);
// getting name of the user
final String name = profile.getString("name");
// getting email of the user
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
#Override
public void run() {
// getProfileInformation();
String arr[] = name.split(" ", 2);
String name1 = arr[0];
String name2 = arr[1];
Intent intent = new Intent(FacebookLogin.this, UserAccount.class);
/*Sending some arguments*/
Bundle bundle = new Bundle();
bundle.putString("UserName",name1);
bundle.putString("Id", email);
intent.putExtras(bundle);
logoutFromFacebook();
startActivity(intent); }
});
} catch (JSONException e) {
e.printStackTrace();
} } }); }
#SuppressWarnings("deprecation")
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
facebook.setAccessToken(null);
facebook.setAccessExpires(0);
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
// User successfully Logged out
} } }); }
}
Please anyone tell me how can I avoid this error...

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();
}
}

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