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



现在我已经在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());
                        }
                    }
                }; 

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

不能只将一个打开的图形对象/操作放在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() {...});

这就是我在应用程序中使用的内容。试试看…

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();
    }

经过许多努力,我终于找到了阅读这些文档的答案

这个和这个

这个代码对我有效

    // 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();

最新更新