Android : unable to post game score on facebook wall in android using libgdx framework

on Sunday, August 17, 2014


I am just trying to make a game where I need to post game score on facebook wall. And I am following libGDX framework. Below is my test activity that just post my custom message on facebook wall.



public class MainActivity extends Activity {

private static final String FB_APP_ID = "1452723631660564";
private static final String FACEBOOK_PERMISSION = "publish_stream";
private static final int FB_AUTH = 2; // ANY INTEGER VALUE
Facebook facebook = new Facebook(FB_APP_ID); //
static String FACEBOOK_UPDATE_MSG;
static String FACEBOOK_UPDATE_FAILURE;
static String FACEBOOK_UPDATE_SUCCESS;
static String FACEBOOK_SIGNIN_FAILED;
Handler fHandler = new Handler();

Map<Object, Object> preferences = new HashMap<Object, Object>();

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


login("Hello facebook test app");
}

@SuppressWarnings("deprecation")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case FB_AUTH:
facebook.authorizeCallback(requestCode, resultCode, data);
break;
default:
finish();
break;
}
}


public void postMessageOnWall(final String msg) {


try {

if (facebook.isSessionValid()) {

new Thread(new Runnable() {

@Override
public void run() {
Bundle parameters = new Bundle();
parameters.putString("message", msg);
parameters.putString("link", "ANDROID_MARKET_LINK"); // or any other
// link
parameters.putString("name", "APP/GAME NAME");
try {
@SuppressWarnings("deprecation")
String response = facebook.request("me/feed", parameters,
"POST");

FACEBOOK_UPDATE_MSG = FACEBOOK_UPDATE_SUCCESS;
fHandler.post(mUpdateFacebookNotification);
} catch (IOException e) {
FACEBOOK_UPDATE_MSG = FACEBOOK_UPDATE_FAILURE;
fHandler.post(mUpdateFacebookNotification);

}
}

});

}



} catch (Exception e) {

Log.e("error-----------", e.getLocalizedMessage());

}

}



final Runnable mUpdateFacebookNotification = new Runnable() {
public void run() {
Toast.makeText(getBaseContext(), FACEBOOK_UPDATE_MSG,
Toast.LENGTH_LONG).show();
}
};

@SuppressWarnings("deprecation")
public void login(final String msg) {
/*String access_token = preferences.getString("facebook_access_token",
null);*/

String access_token = (String) preferences.get("facebook_access_token");
long expires = 0;
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(10000000l);
}
/*
* Only call authorize if the access_token has expired.
*/
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "publish_stream","read_stream", "offline_access" },
FB_AUTH, new DialogListener() {
@Override
public void onComplete(Bundle values) {
/*preferences.putString("access_token",
facebook.getAccessToken());
preferences.putLong("access_expires",
facebook.getAccessExpires());
preferences.flush();*/

preferences.put("access_token", facebook.getAccessToken());
preferences.put("access_expires", facebook.getAccessExpires());
if (msg != "")
postMessageOnWall(msg);
else
OpenFbPage();
}

private void OpenFbPage() {



}

@Override
public void onFacebookError(FacebookError error) {
FACEBOOK_UPDATE_MSG = FACEBOOK_SIGNIN_FAILED;
fHandler.post(mUpdateFacebookNotification);
}

@Override
public void onError(DialogError e) {
FACEBOOK_UPDATE_MSG = FACEBOOK_SIGNIN_FAILED;
fHandler.post(mUpdateFacebookNotification);
}

@Override
public void onCancel() {
}
});
}

}

}


I am not getting any exception/error in above code, but it also doesn't post my custom message on my facebook wall. It just opens an fb application and loading alert over that, nothing else. When I debug that, it neither reach on thread's run() method of postMessageOnWall() , nor I get any response defined in same method. Please guide me where I am wrong. I know I write my whole code related to facebook post in single activity, but it just an test application. Once it is successful, I will segregate the logic.


0 comments:

Post a Comment