facebook - 如何使用 Android SDK 将签到发布到 facebook

标签 facebook facebook-android-sdk android-facebook

这些文档很糟糕。我想发布用户登录 Facebook 的信息。根据文档,创建 checkin 对象已被弃用

https://developers.facebook.com/docs/reference/api/checkin

相反,您应该添加带有位置数据的帖子。这就是我正在尝试做的事情。或者也许我应该尝试发布和开放图表故事?

无论如何,这就是我所拥有的,它基本上是在他们的 SDK 示例中发布帖子的代码,该帖子已创建,但没有附加位置数据。

 private void publishStory() {

    Session session = Session.getActiveSession();

    if (session != null) {
        Bundle placeBundle = new Bundle();
        Bundle locationBundle = new Bundle();
        Bundle postParams = new Bundle();

        locationBundle.putString("latitude",String.valueOf(place.getLat()));
        locationBundle.putString("longitude",String.valueOf(place.getLng()));

        placeBundle.putString("id", place.getPage_id());
        placeBundle.putString("name", place.getName());
        placeBundle.putBundle("location", locationBundle);

        postParams.putBundle("place", placeBundle);
        postParams.putString("message", "test message");

        Request.Callback callback = new Request.Callback() {
            public void onCompleted(Response response) {

                String postId = null;

                try {

                JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();


                    postId = graphResponse.getString("id");
                }
                catch (JSONException e) {
                    Log.i(app.TAG, "JSON error " + e.getMessage());
                }
                catch(Exception e){
                    e.printStackTrace();
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(mContext, error.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(mContext, postId, Toast.LENGTH_LONG).show();
                }
            }
        };

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

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }
    else {
        toast("no session to publish");
    }

}

session 确实具有发布权限,并且它将发布帖子,但只有“测试消息”字符串。地点对象来自 facebook 的服务器,因此它是具有 page_id 的实际地点。当我调试后参数看起来像这样

 Bundle[{message=test message, place=Bundle[{id=171229079554355, location=Bundle[{longitude=-122.434568, latitude=37.797314}], name=The Brixton San Francisco}]}]

最佳答案

      Session.openActiveSession(activity, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session != null && session.isOpened()) {

                // Check for publish permissions
                List<String> permissions = session.getPermissions();
                if (!isSubsetOf(PERMISSIONS, permissions)) {
                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                            activity, PERMISSIONS);
                    session.requestNewPublishPermissions(newPermissionsRequest);
                    return;
                }

                Bundle postParams = new Bundle();
                postParams.putString("message", message);

                    postParams.putString("tags",tag);
                         postParams.putString("place",place_id);



                Request.Callback callback = new Request.Callback() {
                    private String toastmessage;

                    public void onCompleted(Response response) {
                        try {
                            JSONObject graphResponse = response
                                    .getGraphObject().getInnerJSONObject();
                            String postId = null;

                            postId = graphResponse.getString("id");
                        } catch (Exception e) {
                            Log.i("Test", "JSON error " + e.getMessage());
                        }
                        FacebookRequestError error = response.getError();
                        if (error != null) {
                            isPosted(false);
                            Toast.makeText(
                                    activity.getApplicationContext(),
                                    error.getErrorMessage(),
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            isPosted(true);
                            toastmessage = "Posted Successfully";
                            Toast.makeText(activity, toastmessage,
                                    Toast.LENGTH_SHORT).show();
                            {

                            }
                        }
                    }
                };

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

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


        }
    });

关于facebook - 如何使用 Android SDK 将签到发布到 facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21153251/

相关文章:

android - 如果已经登录,最好的方法是跳过 android 中的 facebook 登录页面

android - 使用 Facebook Android SDK 分享 Facebook 页面的帖子

android - Misconfigured App 抱歉,myapp 尚未获准在 App Center 中显示。在安卓应用分享中

javascript - 令人难以置信的 Facebook 评论缩水

objective-c - 使用 Graph API 获取页面的 facebook 事件

c# - 使用 asp.net 进行 Facebook 开发从哪里开始

android - Facebook 无法获取所有用户照片

java - 我在 spring Framework.social.facebook api 中使用 maven build 得到 Facebook Profile class not found 异常

android - 使用 Facebook SDK 仅共享文本

android - 从 facebook 注销按钮捕获注销操作 - facebook-android-sdk :4. 0.0