Android如何在新的sdk 3.0中与Facebook Open Graph共享数据?

时间:2013-12-09 14:21:56

标签: android facebook facebook-opengraph sharing

现在我在Facebook上设置了我的Open Graph应用程序。它已被批准。我试图通过bundle params提交我的“对象”,但我很好奇我如何设置如下所示的bundle param对象。

编辑:

正在创建像这样的对象和动作

Objectsaction  这是分享代码

  void publishToWall() {        
        Session session = Session.getActiveSession();
        if (session != null) {
            Log.i("session ==>", "" +session);
            // Check for publish permissions    
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                Log.i("session permissions ==>", "publishToWall");
                pendingPublishReauthorization = true;
                Session.NewPermissionsRequest newPermissionsRequest = new Session 
                        .NewPermissionsRequest(this, PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest);
                return;
            }   

            try {                   
                RequestBatch requestBatch = new RequestBatch();                     
                Log.i("session requestBatch ==>", "requestBatch");

                JSONObject tropy = new JSONObject();

                tropy.put("type", "thebigtoss:tropy");
                tropy.put("title", "A Game of Thrones");
                tropy.put("url","http://www.thebigtoss.com/assets/app-icon.png");
                tropy.put("description", "supernatural forces are mustering.");

                // Set up object request parameters
                Bundle objectParams = new Bundle();
                objectParams.putString("object", tropy.toString());
                // Set up the object request callback
                Request.Callback objectCallback = new Request.Callback() {
                    @Override
                    public void onCompleted(Response response) {
                        Log.i("objectParams onCompleted ==>", "onCompleted");
                        // Log any response error
                        FacebookRequestError error = response.getError();
                        if (error != null) {                     
                            Log.i(TAG, "objectParams =>" +error.getErrorMessage());
                        }
                    }
                };                            
                // Create the request for object creation
                Request objectRequest = new Request(session, 
                        "me/objects/thebigtoss:trophy", objectParams, HttpMethod.POST, objectCallback);

                // Set the batch name so you can refer to the result
                // in the follow-on publish action request
            //  objectRequest.setBatchEntryName("objectCreate");      

                // Add the request to the batch
                requestBatch.add(objectRequest);

                Bundle actionParams = new Bundle();
                // Refer to the "id" in the result from the previous batch request
                actionParams.putString("trophy", "Action parametery");
                // Turn on the explicit share flag
                actionParams.putString("fb:explicitly_shared", "true");

                // Set up the action request callback
                Request.Callback actionCallback = new Request.Callback() {
                    @Override
                    public void onCompleted(Response response) {
                        // dismissProgressDialog();
                        FacebookRequestError error = response.getError();
                        if (error != null) {
                            Toast.makeText(MainActivity.this.getApplicationContext(),error.getErrorMessage(),Toast.LENGTH_LONG).show();
                        } else {
                            String actionId = null;
                            try {
                                JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                                actionId = graphResponse.getString("id");
                            } catch (JSONException e) {
                                Log.i(TAG, "JSON error "+ e.getMessage());
                            }
                            Toast.makeText(MainActivity.this.getApplicationContext(),actionId, Toast.LENGTH_LONG).show();
                        }
                    }
                };                                
                // Create the publish action request
                Request actionRequest = new Request(session,
                        "me/thebigtoss:win", actionParams, HttpMethod.POST, actionCallback);                                
                // Add the request to the batch
                requestBatch.add(actionRequest);                                
                // Execute the batch request
                requestBatch.executeAsync();

            } catch (JSONException e) {
                //Auto-generated catch block
                e.printStackTrace();
            }               
        }               
    }        

但我没有在我的脸书中分享数据

此错误正在获得12-10 15:26:25.710: I/MainActivity(27667): objectParams =>(#100) conflicting og:type found in path (thebigtoss:trophy) and 'properties' (thebigtoss:tropy)

if (error != null) {
Log.i(TAG, "objectParams =>" +error.getErrorMessage());
的代码行中                             }

Request.Callback objectCallback = new Request.Callback() {
                    @Override
                    public void onCompleted(Response response) {
                        Log.i("objectParams onCompleted ==>", "onCompleted");
                        // Log any response error
                        FacebookRequestError error = response.getError();
                        if (error != null) {                     
                            Log.i(TAG, "objectParams =>" +error.getErrorMessage());
                        }
                    }
                }; 

任何人都知道如何在facebook opengraph中共享使用此代码。

3 个答案:

答案 0 :(得分:3)

你不能只在Bundle中放置一个开放的图形对象/动作。

首先,您需要创建一个OpenGraphObject:

OpenGraphObject object = OpenGraphObject.Factory.createForPost("yournamespace:objectname");
object.setTitle("Sample Trophy");
object.setDescription("...");
//... set more properties as necessary

然后创建一个OpenGraphAction并将对象添加到它:

OpenGraphAction action = OpenGraphAction.Factory.createForPost("thebigtoss:win");
action.setProperty("trophy", object);

最后,使用newPostOpenGraphActionRequest方法发布它:

Request request = Request.newPostOpenGraphActionRequest(session, action, new Callback() {...});

答案 1 :(得分:0)

这就是我在我的应用中使用的内容。试试吧..

protected void postFacebook(final SherlockFragmentActivity activity) {
        Session session = Session.getActiveSession();
        if (!session.isOpened()) {
            AppMsg.makeText(getSherlockActivity(), "Please login to Facebook first.", AppMsg.STYLE_ALERT).show();
            return;
        }
        Bundle params = new Bundle();
        params.putString("name", "App Name");
        params.putString("caption", "Check out my mind-blowing app");
        params.putString("description", "Here goes my long description of the app.");
        params.putString("link", "http://play.google.com/store/apps/details?id=com.my.app");
        params.putString("picture", "http://.../ic_launcher.PNG");

        WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(activity, 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) {
                                AppMsg.makeText(activity, "Posted sucessfully.", AppMsg.STYLE_INFO).show();
                            } else {
                                // User clicked the Cancel button
                                Toast.makeText(activity.getApplicationContext(), "Publish cancelled",
                                        Toast.LENGTH_SHORT).show();
                            }
                        } else if (error instanceof FacebookOperationCanceledException) {
                            // User clicked the "x" button
                            Toast.makeText(activity.getApplicationContext(), "Publish cancelled", Toast.LENGTH_SHORT)
                                    .show();
                        } else {
                            // Generic, ex: network error
                            Toast.makeText(activity.getApplicationContext(), "Error posting story", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }

                }).build();
        feedDialog.show();
    }

答案 2 :(得分:0)

最后很多努力我找到了阅读这些文件的答案

THISTHIS

此代码为我工作

    // Set up object request parameters

            RequestBatch requestBatch = new RequestBatch();                     

            Bundle objectParams = new Bundle();
            objectParams.putString("type", type);
            objectParams.putString("title", title);
            objectParams.putString("url", dataurl);                  
            objectParams.putString("description",  description);
            objectParams.putString("image",  imagepath);         

            // Set up the object request callback
            Request.Callback objectCallback = new Request.Callback() {
                @Override
                public void onCompleted(Response response) {
                    Log.i("objectParams onCompleted ==>", "onCompleted");
                    // Log any response error
                    FacebookRequestError error = response.getError();
                    if (error != null) {                     
                        Log.i(TAG, "objectParams =>" +error.getErrorMessage());
                    }
                }
            };                       
            Log.i(TAG, " "+  "me/objects/thebigtoss:trophy");
            // Create the request for object creation
            Request objectRequest = new Request(session, 
                    "me/objects/"objectadata"", objectParams, HttpMethod.POST, objectCallback);                            

            // Set the batch name so you can refer to the result
            // in the follow-on publish action request              
            Log.i(TAG, "objectRequest ==>"+ "objectRequest");
            //objectRequest.setBatchEntryName("objectCreate");                    
            // Add the request to the batch
            requestBatch.add(objectRequest);      


            Bundle actionParams = new Bundle();
            Log.i(TAG, "actionParams ==>"+ "actionParams");
            // Refer to the "id" in the result from the previous batch request               
            actionParams.putString("trophy", dataurl);       
            actionParams.putString("image",  imagepath);                 

            // Set up the action request callback
            Request.Callback actionCallback = new Request.Callback() {
                @Override
                public void onCompleted(Response response) {
                    Log.i(TAG, "actionParams onCompleted==>"+ "actionParams onCompleted");
                    // dismissProgressDialog();
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Log.i(TAG, "actionParams onCompleted if==>"+ error.getErrorMessage());
                        Toast.makeText(TrophyDetailsActivity.this.getApplicationContext(),error.getErrorMessage(),Toast.LENGTH_LONG).show();
                    } else {
                        Log.i(TAG, "actionParams onCompleted else==>"+ "actionParams onCompleted else");

                        String actionId = null;
                        try {
                            JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                            actionId = graphResponse.getString("id");
                        } catch (JSONException e) {
                            Log.i(TAG, "JSON error "+ e.getMessage());
                        }
                        Log.i("actionId ==>", actionId);
                    }
                }
            };                                
            // Create the publish action request
            Request actionRequest = new Request(session,
                    "me/action", actionParams, HttpMethod.POST, actionCallback);                                
            // Add the request to the batch
            requestBatch.add(actionRequest);                                
            // Execute the batch request
            requestBatch.executeAsync();