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

Posted

技术标签:

【中文标题】Android 如何在新的 sdk 3.0 中与 Facebook Open Graph 共享数据?【英文标题】:Android how to share data with Facebook Open Graph in new sdk 3.0? 【发布时间】:2013-12-09 14:21:55 【问题描述】:

现在我已经在 Facebook 上设置了我的 Open Graph 应用程序。它已被批准。我正在尝试通过捆绑参数提交我的“对象”,但我很好奇如何设置捆绑参数对象,如下所示。

编辑:

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

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

但我没有在我的 facebook 中分享数据

这个错误是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 中使用此代码进行分享。

【问题讨论】:

【参考方案1】:

您不能只将打开的图形对象/操作放入 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() ...);

【讨论】:

再次,您将打开图形操作直接放入包中,这不会导致正确的序列化。为什么不导入 OpenGraphObject?请尝试我上面直接粘贴的代码。 如果类没有被导入,那么这是一个不同的问题。检查您的项目设置(以及您如何使用 Facebook SDK)。 OpenGraphObject 是 SDK 的一部分。【参考方案2】:

这是我在我的应用程序中使用的。试试吧。。

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

【讨论】:

嗨@NagarjunaReddy 你读过这个吗? developers.facebook.com/docs/android/share-using-the-object-api 正如 Mingli 所说,你不能只将一个开放的图形对象/动作放在一个 Bundle 中。 您提供的网址是否正确?您提供的 URL 必须可供 Facebook 访问,否则您将收到此错误。 在上面的url中检查Prerequisites。 更改为有效的 url 但仍然发生同样的事情。【参考方案3】:

终于找到了阅读本文档的答案

THIS 和 THIS

这段代码对我有用

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

【讨论】:

以上是关于Android 如何在新的 sdk 3.0 中与 Facebook Open Graph 共享数据?的主要内容,如果未能解决你的问题,请参考以下文章

Facebook 安卓 sdk 3.0 SSO

如何在新的 android 设计库中使用 TextInputLayout

Android向后兼容性

在新的 Facebook JavaScript SDK 中显示弹出窗口

Android Studio 3.0 Kotlin 更改未反映在构建中

使用 Facebook-Android SDK 3.0 正确请求发布权限