简体   繁体   English

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

[英]Android how to share data with Facebook Open Graph in new sdk 3.0?

Right now I have setup my Open Graph Application on Facebook. 现在我在Facebook上设置了我的Open Graph应用程序。 It has been approved. 它已被批准。 I am trying to submit my "objects" via the bundle params but I am curious how I setup a bundle param object like the following. 我试图通过bundle params提交我的“对象”,但我很好奇我如何设置如下所示的bundle param对象。

EDIT: 编辑:

Am creating object and action like this 我正在创建像这样的对象和动作

Objects and action this is code for sharing 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();
            }               
        }               
    }        

But am not getting sharing data in my facebook 但我没有在我的脸书中分享数据

this error am getting 12-10 15:26:25.710: I/MainActivity(27667): objectParams =>(#100) conflicting og:type found in path (thebigtoss:trophy) and 'properties' (thebigtoss:tropy) 这个错误得到12-10 15:26:25.710: I/MainActivity(27667): objectParams =>(#100) conflicting og:type found in path (thebigtoss:trophy) and 'properties' (thebigtoss:tropy)

in this code line of if (error != null) {
Log.i(TAG, "objectParams =>" +error.getErrorMessage());
if (error != null) {
Log.i(TAG, "objectParams =>" +error.getErrorMessage());
代码行中if (error != null) {
Log.i(TAG, "objectParams =>" +error.getErrorMessage());
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());
                        }
                    }
                }; 

Any one have idea how to share useing this code in facebook opengraph. 任何人都知道如何在facebook opengraph中共享使用此代码。

You can't just put an open graph object/action in a Bundle. 您不能只在Bundle中放置一个打开的图形对象/动作。

First you need to create an OpenGraphObject: 首先,您需要创建一个OpenGraphObject:

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

Then create an OpenGraphAction and add your object to it: 然后创建一个OpenGraphAction并将对象添加到它:

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

Finally, post it using the newPostOpenGraphActionRequest method: 最后,使用newPostOpenGraphActionRequest方法发布它:

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

This is what I am using in my app. 这就是我在我的应用程序中使用的内容。 Try it.. 试试吧..

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

Finally lot of struggle i found the answer for reading this documentations 最后,我找到了阅读这些文件的答案

THIS and THIS 这个这个

this code working for me 这段代码对我有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM