从android获取Facebook SDK 3.0.1中的读取和发布权限

时间:2013-05-24 17:06:37

标签: android facebook-android-sdk

我正在尝试在Android应用中获得我的阅读和发布权限,它在网络对话框中第一次完美运行。下次成功授权后,表示您已经授权对话两次。

以下是创建会话对象的代码。

Session session = Session.getActiveSession();
                if(session == null){
                    session = new Session.Builder(HomeActivity.this).setApplicationId(MyTouchTunesApplication.getSettings().getFacebookAppId()).build();
                    Session.setActiveSession(session);
                }else{
                    Util.clearFacebookToken();
                 }
                if(!session.isOpened() && !session.isClosed()){
                    ArrayList<String> permissions = new  ArrayList<String>();
                    permissions.add("email");
                    session.openForRead(new Session.OpenRequest(HomeActivity.this).setCallback(statusCallback).setPermissions(permissions));
                }else{
                    Session.setApplicationId(MyTouchTunesApplication.getSettings().getFacebookAppId());
                    Session.openActiveSession(HomeActivity.this, true, statusCallback);
                }

我的回调方法

public void call(Session session, SessionState state, Exception exception) {
        Log.d("session state", state.toString());
        if(exception != null){
            if(!exception.getMessage().contains("user")){
                showError(exception.getMessage());
                return;
            }
        }
        if(session.isOpened() && state == SessionState.OPENED && !session.getPermissions().contains("publish_stream")){
            final String[] PERMISSION_ARRAY_PUBLISH = {"publish_stream"};
            final List<String> permissionList = Arrays.asList(PERMISSION_ARRAY_PUBLISH);
            session.requestNewPublishPermissions(new NewPermissionsRequest(HomeActivity.this,permissionList ));
            return;
        }
        if(session !=null && session.isOpened() && state == SessionState.OPENED_TOKEN_UPDATED){
            mLoginCommand = new LoginCommand(HomeActivity.this, fbloginHandler, null, null, session.getAccessToken(), String.valueOf(session.getExpirationDate().getTime()));
            mLoginCommand.execute();
        }
    }

当用户在授权应用程序后进行日志记录时,我没有获得所有权限,它只返回电子邮件权限,这就是我的状态在Statuscallback中出现故障的原因。我必须逐个收到电子邮件并发布,当用户发布status.i时,我不能对此实现表示怀疑,希望能有所帮助

感谢。

2 个答案:

答案 0 :(得分:2)

我已经实现了这样的工作。

private void doSocialNetworkinWithFacebook()
    {
        // check for session 
         Session session=Session.getActiveSession();
         if (session != null && session.isOpened()) 
             {  
                // user is already login show
                    try
                        {
                            Session.OpenRequest request = new Session.OpenRequest(this);
                            request.setPermissions(Arrays.asList("email", "publish_actions"));
                        }
                    catch (Exception e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                 Request.executeMeRequestAsync(session, new Request.GraphUserCallback() 
                        {
                          // callback after Graph API response with user object

                          @Override
                          public void onCompleted(GraphUser user, Response response) 
                          {
                              if (user != null) 
                               {
                                   Toast.makeText(activity, "Welcome  "+user.getName(), Toast.LENGTH_SHORT).show();
                                  // publishFeedDialog(session);
                                    try
                                        {
                                            strFirstName = user.getFirstName().toString();
                                            strLocation = user.getLocation().getProperty("name").toString();
                                            strEmail = user.asMap().get("email").toString();

                                        }
                                    catch (Exception e)
                                        {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                            strEmail="";
                                        }

                                    runOnUiThread(new Runnable()
                                        {
                                            public void run()
                                                {
                                                    setUserInfoFromFacebook(strFirstName, strLocation, strEmail);
                                                }
                                        });
                                }
                          }
                    });

             }
         else
             {
                 // user is not log in 
                 //show  login screen

                // start Facebook Login

                    try
                        {
                            Session.OpenRequest request = new Session.OpenRequest(this);
                            request.setPermissions(Arrays.asList("email", "publish_actions"));
                        }
                    catch (Exception e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                 Session.openActiveSession(activity, true, new Session.StatusCallback() 
                 {

                     // callback when session changes state
                    @Override
                    public void call(final Session session, SessionState state, Exception exception) 
                    {
                        //session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("email")));
                        Log.d(TAG, "Session :"+session.toString());
                        Log.d(TAG, "Session is opened :"+session.isOpened());

                        if (session.isOpened()) 
                        {                               
                            // make request to the /me API

                            Request.executeMeRequestAsync(session, new Request.GraphUserCallback()

                                {
                                  // callback after Graph API response with user object
                                  @Override
                                  public void onCompleted(GraphUser user, Response response) 
                                  {
                                      if (user != null) 
                                       {

                                           Toast.makeText(activity, "Welcome  "+user.getName(), Toast.LENGTH_SHORT).show();
                                          // publishFeedDialog(session);
                                           try
                                            {
                                                    strFirstName = user.getFirstName().toString();
                                                    strLocation = user.getLocation().getProperty("name").toString();
                                                    strEmail = user.asMap().get("email").toString();
                                            }
                                        catch (Exception e)
                                            {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                                strEmail="";
                                            }

                                            runOnUiThread(new Runnable()
                                                {
                                                    public void run()
                                                        {
                                                            setUserInfoFromFacebook(strFirstName, strLocation, strEmail);
                                                        }
                                                });
                                        }
                                  }
                            });

                        }
                        else if(session.isClosed())
                            {
                                 Toast.makeText(activity, "Unable to connect facebook, please try later..",Toast.LENGTH_SHORT).show();
                            }

                    }
                  });
             }

    }

答案 1 :(得分:1)

使用以下内容替换创建会话代码中的第二个if-else条件:

        if (!session.isOpened() && !session.isClosed()) {
             Session.OpenRequest request = new Session.OpenRequest(this);
                request.setPermissions(Arrays.asList("email", "publish_actions"));
                request.setCallback(statusCallback);
                session.openForPublish(request);
           }else{
                Session.setApplicationId(MyTouchTunesApplication.getSettings().getFacebookAppId());
                Session.openActiveSession(HomeActivity.this, true, statusCallback);
            }

现在应该可以了。