Giridaran Regunathan
Giridaran Regunathan

Reputation: 61

Getting read and publish permission in Facebook SDK 3.0.1 from android

I am trying to get my Read and publish permission in Android app, its working perfectly on first time in web dialog. After successfully authorized the next time its says you have already authorized dialog twice.

Here is the code for creating Session Object.

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

My Callback method

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

i am not getting the all permission when user is logging after authorized the application, its just return only email permission, that's why my condition is breaking in Statuscallback. i have to get the email and publish one by one, cant do that when user post the status.i doubted about this implementation, expecting some help in this

Thanks.

Upvotes: 1

Views: 8831

Answers (2)

Pankaj Patil
Pankaj Patil

Reputation: 105

I have implemented like this and it works.

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

                    }
                  });
             }

    }

Upvotes: 2

Avi Singh
Avi Singh

Reputation: 171

Replace your second if-else condition in the creating session code with this one:

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

It should work now.

Upvotes: 1

Related Questions