1

我正在尝试通过以下代码在用户提要中共享一个链接,并且只有我使用开发人员帐户登录才能使用。如果我尝试通过任何其他用户帐户发布它,它只会给出一个帖子 ID,但没有可见的帖子。

MainFragment 仅包含布局充气器,该布局只有一个 TextView 和 facebook LoginButton

package com.chamika.fbtest;

import java.util.Arrays;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.FacebookRequestError;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionDefaultAudience;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;

public class MainActivity extends FragmentActivity {

    private UiLifecycleHelper uiHelper;
    private Context context;

    private static final String TAG = "MainFragment";

    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions","publish_stream");
    private static final int REAUTH_ACTIVITY_CODE = 100;

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    private MainFragment mainFragment;

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

        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        context = this;

        if (savedInstanceState == null) {
            // Add the fragment on initial activity setup
            mainFragment = new MainFragment();
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
        } else {
            // Or set the fragment from restored state info
            mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
        // Session.getActiveSession().onActivityResult(this, requestCode,
        // resultCode, data);
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        final TextView text = (TextView) findViewById(R.id.welcome);
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");
            text.setText("Logged in...");
            text.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    publish();
                }
            });

        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
            text.setText("Logged out...");
            text.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    text.setText("Please log");
                }
            });
        }
    }

    private void publish() {
        Session session = Session.getActiveSession();

        if (session == null || !session.isOpened()) {
            return;
        }

        List<String> permissions = session.getPermissions();
        if (!permissions.containsAll(PERMISSIONS)) {
            requestPublishPermissions(session);
            return;
        }

        // Show a progress dialog because sometimes the requests can take a
        // while.
        final ProgressDialog progressDialog = ProgressDialog.show(this, "requesting permission",
                "requesting additional permission", true);

        Bundle postParams = new Bundle();
        postParams.putString("name", "Test post");
        postParams.putString("caption", "This is a test post");
        postParams.putString("description",
                "Description of the test post");
        postParams.putString("link", "https://developers.facebook.com/android");
        postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

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

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

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

    }

    private void requestPublishPermissions(Session session) {
        if (session != null) {
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS)
            // demonstrate how to set an audience for the publish permissions,
            // if none are set, this defaults to FRIENDS
                    .setDefaultAudience(SessionDefaultAudience.FRIENDS).setRequestCode(REAUTH_ACTIVITY_CODE);
            session.requestNewPublishPermissions(newPermissionsRequest);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        // Session session = Session.getActiveSession();
        // if (session != null && (session.isOpened() || session.isClosed())) {
        // onSessionStateChange(session, session.getState(), null);
        // }
        uiHelper.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

}
4

4 回答 4

2

默认情况下,应用程序处于沙盒模式(仅对管理员、开发人员和测试人员可见)

在此处输入图像描述

禁用沙盒模式。(对所有用户可见)

在此处输入图像描述

于 2013-08-12T11:45:36.043 回答
0

您必须添加显式共享参数 ( fb:explicitly_shared)。

本页所述:“明确共享的操作有资格在新闻提要中显示为独立故事”。
这件事适用于 Open Graph 规范,但我认为这在你的情况下也是有效的。

于 2013-07-04T10:34:43.773 回答
0

我假设您可能忘记将测试人员权限添加到您尝试过的 Facebook 帐户,而不是开发人员帐户。在您开发应用程序时,它将处于沙盒模式,只有开发人员帐户和测试人员帐户才能访问。转到 developer.facebook.com 中的应用程序并添加测试人员帐户。

于 2013-06-23T15:37:06.970 回答
0
Bundle params = new Bundle();
    params.putString("name", "Your App Name or any desired name");
    params.putString("description", "Message to share");
    params.putString("picture", "Image Url");

    WebDialog feedDialog = (
        new WebDialog.FeedDialogBuilder(context,
            Session.getActiveSession(),
            params))
        .setOnCompleteListener(new OnCompleteListener() {

            @Override
            public void onComplete(Bundle values,
                FacebookException error) {
                if (error == null) {
                    // When the story is posted, echo the success
                    // and the post Id.
                    final String postId = values.getString("post_id");
                    if (postId != null) {
                        FacebookHelper.this.mShareDelegate
                        .shareStatus();
                    } else {
                        // User clicked the Cancel button

                    }
                } else if (error instanceof FacebookOperationCanceledException) {
                    // User clicked the "x" button

                } else {
                    // Generic, ex: network error

                }
            }

        })
        .build();
    feedDialog.show();
于 2014-08-26T11:16:54.110 回答