0

我正在尝试从我的 Android 应用程序在 Facebook 墙上发布一些预定义的消息和图像。

我做了什么

我使用 openssl 创建哈希键并将其放入我Facebook developer app(Native Android App)还设置的包名称和活动名称中。然后我在我的应用程序中使用 Facebook 应用程序密钥。
我完成了所有编码并运行我的应用程序,它要求我登录,然后我输入用户名和密码,但是当我点击发布消息按钮时,它显示Facebook状态页面,我需要手动输入我不想要的状态。我想在不打开此页面的情况下直接在墙上发布预定义的消息。

这是我的代码供您参考。

 private void postStatusUpdate() {
        if (canPresentShareDialog) {
            FacebookDialog shareDialog = createShareDialogBuilder().build();
            uiHelper.trackPendingDialogCall(shareDialog.present());
        } else if (user != null && hasPublishPermission()) {
            Bundle postParams = new Bundle();
            GetSet gs = new GetSet();               
            postParams.putString("name", gs.getFacebook_menu_name());
            postParams.putString("caption", gs.getFacebook_hotel_name());
            postParams.putString("description", "My rating for this menu is "+gs.getFacebook_menu_rating());
            //postParams.putString("link", "https://developers.facebook.com/android");
            postParams.putString("picture", gs.getFacebook_url_address());


            Request.Callback callback= new Request.Callback() {
                public void onCompleted(Response response) {
                    JSONObject graphResponse = response
                                               .getGraphObject()
                                               .getInnerJSONObject();
                    String postId = null;
                    try {
                        postId = graphResponse.getString("id");
                    } catch (JSONException e) {
                        Log.i("TAG",
                            "JSON error "+ e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Toast.makeText(FacebookActivity.this
                                .getApplicationContext(),error.getErrorMessage(),Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(FacebookActivity.this
                                    .getApplicationContext(), 
                                    "Details suuccesfully post on your wall",
                                    Toast.LENGTH_LONG).show();

                    }
                }
            };

            Request request = new Request(Session.getActiveSession(), "me/feed", postParams, 
                                  HttpMethod.POST, callback);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();

        } else {
            pendingAction = PendingAction.POST_STATUS_UPDATE;
        }
    }  

这里有两个条件。
一个。如果我的手机没有 Facebook 应用程序,那么它可以正常工作。无需手动输入即可在 Facebook 墙上发布消息

。但是,如果我在我的手机中安装了 Facebook 应用程序,那么它会打开状态页面,我在其中手动输入了我不想要的消息。

我不明白必须做什么。因为我在某处阅读 Facebook 不允许在不打开状态页面的情况下发布消息。

请给我任何参考或提示。

4

2 回答 2

0

这个类可以帮助我在没有对话框的情况下在我的 Facebook 墙上发送消息:

public class FBManager{

private static final String FB_ACCESS_TOKEN = "fb_access_token";
private static final String FB_EXPIRES = "fb_expires";

private Activity context;
private Facebook facebookApi;

private Runnable successRunnable=new Runnable(){
@Override
public void run() {
    Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();
}
};    

public FBManager(Activity context){

this.context = context;

facebookApi = new Facebook(FB_API_ID);

facebookApi.setAccessToken(restoreAccessToken());

} 

public void postOnWall(final String text, final String link){
new Thread(){
    @Override
    public void run(){
        try {
            Bundle parameters = new Bundle();
            parameters.putString("message", text);
            if(link!=null){
                parameters.putString("link", link);
            }
            String response = facebookApi.request("me/feed", parameters, "POST");
            if(!response.equals("")){
                if(!response.contains("error")){
                    context.runOnUiThread(successRunnable);                     
                }else{
                    Log.e("Facebook error:", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}.start();     
}


public void save(String access_token, long expires){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor=prefs.edit();
editor.putString(FB_ACCESS_TOKEN, access_token);
editor.putLong(FB_EXPIRES, expires);
editor.commit();
}

public String restoreAccessToken(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString(FB_ACCESS_TOKEN, null);      
}

public long restoreExpires(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getLong(FB_EXPIRES, 0);        
} 
于 2013-10-24T12:55:26.787 回答
-1

您需要使用单点登录进行 Facebook 登录 - SessionLoginBehavior.SSO_WITH_FALLBACK;

于 2013-08-29T07:30:07.890 回答