Announcement Announcement Module
Collapse
No announcement yet.
facebook app requests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • facebook app requests

    Is there a way to obtain and manage 'app requests' abstractions? looking at fb's Request 2.0 model, I think this is an interesting feature to have in spring-social.

    If this is not currently possible, how can I get the access token to query the graph api directly?
    Last edited by aramkhas; May 20th, 2011, 02:50 AM.

  • #2
    You can query the Graph api directly by working with the GraphApi super-interface. You can also get a facebookTemplate.getRestTemplate() and invoke at that low-level. Each request is signed with the access token for you. You don't have to worry about that.

    I would consider forking spring-social-facebook on github, designing support for the functionality you need into the Facebook API binding, and submitting a pull request back. Check out the wiki for the root Spring Social project for Github contribution guidelines.

    Keith

    Comment


    • #3
      Thanks Keith. I would love to code it for spring social once I get more insight on it.

      For now, I managed to make it work with the FacebookTemplate.

      One thing I am not sure is what is the best way to grab the access_token from within a controller. I started my project using the showcase sample app. I tried @FacebookCookieValue but it always gives me a null string.

      controllers.xml:
      <bean id="facebookWebArgResolver" class="org.springframework.social.facebook.web.Fac ebookWebArgumentResolver">
      <constructor-arg value="${facebook.appId}" />
      <constructor-arg value="${facebook.appSecret}" />
      </bean>

      <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter">
      <property name="customArgumentResolver" ref="facebookWebArgResolver"/>
      </bean>


      Controller class:
      @RequestMapping(value="/group/save", method=RequestMethod.POST)
      public String save(@FacebookCookieValue(value="access_token") String access_token, ...



      The only (horrible) way I made it to obtain that token is in the ConnectionRepositoryConfig , through the findPrimaryConnectionToApi (I would like to change I have done this).
      I wonder how to do this all the way in the controller.


      @Configuration
      public class ConnectionRepositoryConfig {

      @Inject
      private UsersConnectionRepository usersConnectionRepository;

      @Bean
      @Scope(value="request")
      public ConnectionRepository connectionRepository(@Value("#{request.userPrincip al}") Principal principal) {
      if (principal == null) {
      throw new IllegalStateException("Unable to get a ConnectionRepository: no user logged in");
      }
      ConnectionRepository createConnectionRepository = usersConnectionRepository.createConnectionReposito ry(principal.getName());
      Connection<FacebookApi> findPrimaryConnectionToApi = createConnectionRepository.findPrimaryConnectionTo Api(FacebookApi.class);
      User user = ((FacebookAuthentication) principal).getUser();
      if (user.getAccessToken() == null && findPrimaryConnectionToApi != null) {
      user.setAccessToken(findPrimaryConnectionToApi.cre ateData().getAccessToken());
      }
      return createConnectionRepository;
      }

      }


      my controller has access to that user object and that's how I get to the AppRequest object (a simple pojo i wrote):

      RestTemplate restTemplate = new FacebookTemplate(user.getAccessToken()).getRestTem plate();
      AppRequest appRequest = restTemplate.getForEntity("https://graph.facebook.com/" + appRequestId, AppRequest.class).getBody();

      Comment


      • #4
        Well, you generally don't want to be working with the access token directly; rather, simply work through the Connection API. Simply establish a Connection to Facebook, call getApi() to get a handle to the Facebook API binding interface, and consult GraphApi to see if it can be used to invoke what you need. If not, downcast the Facebook API binding interface to FacebookTemplate and work with the underlying RestTemplate. The Connection shields you from having to deal with access token details and acts as a factory for the API binding instance. See the ref docs for more information.

        Keith

        Comment


        • #5
          nice! I replaced all the messy code with this line in my controller method

          AppRequest appRequest = connectionRepositoryProvider.get().findPrimaryConn ectionToApi(FacebookApi.class).getApi().fetchObjec t(appRequestId, AppRequest.class);

          I am not sure what findPrimaryConnectionToApi in the context of a logged in user to my app... but it sounds like it does get me the current connection to facebook for my logged in user.

          Thank you Keith!

          Comment


          • #6
            The JavaDoc and ref docs should tell you Glad you got it working!

            Comment


            • #7
              I can't find the AppRequest class, am using spring-social Facebook 1.0.0. Can you pl. let me know which version has this class or is there any alternative.

              Comment


              • #8
                The AppRequest is not part of Spring Social and is something that the original poster had created themselves. I'm not sure which of the objects at https://developers.facebook.com/docs/reference/api/ they were mapping it to, otherwise I'd be happy to add it to the API binding. If the original poster is still around, maybe they can shed some light on this. (And I might try some experimentation to see if I can figure it out.)

                Comment


                • #9
                  ApplicationRequest class definition

                  After digging around, i figured out this class. This works too, thought to share this, so you can include in spring social api.


                  /**
                  * sample json data of application request
                  {
                  "id": "xxxxx",
                  "application": {
                  "name": "aaaa",
                  "id": ""
                  },
                  "to": {
                  "name": "",
                  "id": "12312"
                  },
                  "from": {
                  "name": "Kotla",
                  "id": "123112"
                  },
                  "data": "",
                  "message": "Get in the action now by clicking on Accept Button. ",
                  "created_time": "2012-09-08T06:32:42+0000"
                  }
                  * @author skotla
                  *
                  */


                  public class ApplicationRequest {

                  private String data;

                  private String message;

                  private String id;

                  private String created_time;



                  public String getCreated_time() {
                  return created_time;
                  }

                  public void setCreated_time(String created_time) {
                  this.created_time = created_time;
                  }

                  private Application application;

                  private From from;

                  private To to;




                  public Application getApplication() {

                  return application;
                  }

                  public void setApplication(Application application) {
                  this.application = application;
                  }

                  public From getFrom() {
                  return from;
                  }

                  public void setFrom(From from) {
                  this.from = from;
                  }

                  public To getTo() {
                  return to;
                  }

                  public void setTo(To to) {
                  this.to = to;
                  }

                  public String getData() {
                  return data;
                  }

                  public void setData(String data) {
                  this.data = data;
                  }

                  public String getMessage() {
                  return message;
                  }

                  public void setMessage(String message) {
                  this.message = message;
                  }

                  public String getId() {
                  return id;
                  }

                  public void setId(String id) {
                  this.id = id;
                  }


                  public static class Application{
                  private String name, id;


                  public String getName() {
                  return name;
                  }

                  public void setName(String name) {
                  this.name = name;
                  }

                  public String getId() {
                  return id;
                  }

                  public void setId(String id) {
                  this.id = id;
                  }

                  }

                  public static class From{
                  private String name, id;

                  public String getName() {
                  return name;
                  }

                  public void setName(String name) {
                  this.name = name;
                  }

                  public String getId() {
                  return id;
                  }

                  public void setId(String id) {
                  this.id = id;
                  }

                  }

                  public static class To{
                  private String name, id;

                  public String getName() {
                  return name;
                  }

                  public void setName(String name) {
                  this.name = name;
                  }

                  public String getId() {
                  return id;
                  }

                  public void setId(String id) {
                  this.id = id;
                  }


                  }



                  }

                  Comment

                  Working...
                  X