private Session.NewPermissionsRequest buildPermissionsRequest(
      JSONArray args, final CallbackContext callbackContext) throws JSONException {
    final List<String> permissions = asStringList(args.getJSONArray(0));

    Session.NewPermissionsRequest permissionsRequest =
        new Session.NewPermissionsRequest(cordova.getActivity(), permissions);

    permissionsRequest.setCallback(
        new StatusCallback() {
          @Override
          public void call(Session session, SessionState state, Exception exception) {
            try {
              session.removeCallback(this);

              if (missingPermission(permissions)) {
                callbackContext.success("not_authorized");
              } else {
                callbackContext.success("authorized");
              }
            } catch (Exception e) {
              Log.e(TAG, "Error executing action", e);
              callbackContext.error(e.toString());
            }
          }
        });

    return permissionsRequest;
  }
 private static Session.NewPermissionsRequest getNewPermissionsRequest(
     Session session, StatusCallback callback, List<String> permissions, Activity activity) {
   Session.NewPermissionsRequest req = new Session.NewPermissionsRequest(activity, permissions);
   req.setCallback(callback);
   // This should really be "req.setCallback(callback);"
   // Unfortunately the current underlying SDK won't add the callback when you do it that way
   // TODO: when upgrading to the latest see if this can be "req.setCallback(callback);"
   // if it still doesn't have it, file a bug!
   session.addCallback(callback);
   req.setDefaultAudience(SessionDefaultAudience.FRIENDS);
   return req;
 }
  private void requestPublishPermissions(JSONArray args, final CallbackContext callbackContext)
      throws JSONException {
    Session.NewPermissionsRequest permissionsRequest =
        buildPermissionsRequest(args, callbackContext);

    String audienceArg = args.getString(1);
    SessionDefaultAudience audience = SessionDefaultAudience.NONE;

    if ("friends".equals(audienceArg)) {
      audience = SessionDefaultAudience.FRIENDS;
    } else if ("only_me".equals(audienceArg)) {
      audience = SessionDefaultAudience.ONLY_ME;
    } else if ("everyone".equals(audienceArg)) {
      audience = SessionDefaultAudience.EVERYONE;
    }

    permissionsRequest.setDefaultAudience(audience);

    Session.getActiveSession().requestNewPublishPermissions(permissionsRequest);
  }