Android Facebook共享对话框自定义故事与网址和图像

时间:2014-11-20 12:21:18

标签: java android facebook facebook-graph-api

您可以在下面找到我使用开放图表和一些自定义字段共享自定义故事的代码。图片和网址无法正常工作 - 我收到错误:com.facebook.FacebookException,在添加网址时没有更多内容,当我只设置图片时我会收到Toast with" Unidentified error"消息:

OpenGraphObject spent = OpenGraphObject.Factory.createForPost("namespace:spent");
    spent.setProperty("og:type", "namespace:" + categorySlug.replace("-", "_"));
    //Not working -- spent.setProperty("og:url", "http://example.com");
    //Not working -- spent.setUrl("http://example.com");
    //Not working -- spent.setProperty("og:image", "http://example.com/image.png");
    //Not working -- spent.setImageUrls(Arrays.asList("http://example.com/image.png"));
    spent.setProperty("og:title", "Title");
    spent.setProperty("og:description", "Description");
    spent.setProperty("namespace:hours", container.getHours());
    spent.setProperty("namespace:minutes", container.getMinutes());
    spent.setProperty("namespace:seconds", container.getSeconds());
    spent.setType("namespace:mycustomobject");

    OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
    action.setProperty("mycostomobject", spent);
    action.setType("namespace:spent");

final FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(
                this,
                action,
                "mycustomobject"
).build();

if(FacebookDialog.canPresentShareDialog(context,FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
    uiLifecycleHelper.trackPendingDialogCall(shareDialog.present());
}

图像显示在预览中。但是当我点击发布时,我收到了上面提到的错误。 但是,我使用JSON代码的请求正在工作并共享图像:

StringBuilder sb = new StringBuilder();
    sb.append("{")
            .append("\"").append("og:type").append("\":\"").append("namespace:").append(categorySlug.replace("-", "_")).append("\",")
            .append("\"").append("og:url").append("\":\"").append("http://example.com").append("\",")
            .append("\"").append("og:title").append("\":\"").append("Title").append("\",")
            .append("\"").append("og:image").append("\":\"").append("http://example.com/icon.png").append("\",")
            .append("\"").append("og:description").append("\":\"").append("Desc").append("\",")
            .append("\"").append("namespace:hours").append("\":\"").append(container.getHours()).append("\",")
            .append("\"").append("namespace:minutes").append("\":\"").append(container.getMinutes()).append("\",")
            .append("\"").append("namespace:seconds").append("\":\"").append(container.getSeconds()).append("\"")
            .append("}");

final Bundle object = new Bundle();
    object.putString(categorySlug.replace("-","_"), sb.toString());
    object.putBoolean("fb:explicitly_shared", true);

    Request request = new Request(
            Session.getActiveSession(),
            "me/namespace:spent",
            object,
            HttpMethod.POST,
            new Request.Callback() {
                @Override
                public void onCompleted(Response response) {
                    if(response != null && response.getError() != null) Log.d("Facebook Share", "Error message:" + response.getError().getErrorCode() + ", " + response.getError().getErrorMessage());
                    if(response != null && response.getGraphObject() != null){
                        Toast.makeText(context, "Success!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    );
request.executeAsync();

注意: 图像存在于服务器中。 我更改了名称空间和域名,因此名称中可能存在错误

取消注释addProperty时出现异常(" og:image",""):https://gist.github.com/Averus89/55a86b3618f143926eb2

0 个答案:

没有答案