Announcement Announcement Module
No announcement yet.
How to use scores api Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use scores api

    I want to user facebook scores api ( ) but I obviously need user access token which seems a little complicated to get.
    Is there any way to simply use scores api on server side? I definitely don't want to handle it on the client.

    I couldn't manage it using facebook.openGraphOperations().publishAction()
    So any suggestions? solutions?

  • #2
    The scores API isn't part of the Spring Social Facebook API binding yet, but you can always use the RestOperations from the Facebook interface's restOperations() method to request anything you want. Or use the methods from GraphOperations for a slightly higher-level programming model.

    But, from what I know about the scores API, you *will* need an access token. It'll either need to be a user access token (obtained via a typical OAuth2 authorization code grant flow...involving browser redirects), or an app access token (obtained via OAuth2Operations's authenticateClient() method). Once you have that token, you can create an instance of FacebookTemplate (passing in the token as a constructor argument) and go from there.


    • #3
      considering a scenario in which user has already signed in the game, is there any way that i can get the access token from spring social api?


      • #4
        I'm afraid that I don't know enough about FB game development and the scores API to provide an effective answer. It's certainly something I'll be looking into later this year, but at the moment I only know what I know from scanning the scores API documentation.


        • #5
          Well in this case it is not about knowing scores api it is just about how to get access token for a signed in user from spring social fb api.
          Really can I get the access token?


          • #6
            Okay, misunderstood the question. I'm still unsure if I can directly answer your question, though, because I'm unsure when/where you need to get the access token.

            First, may I assume that you're using either ConnectController to establish connections or ProviderSignInController for user authentication? If so, then there'll be a connection and you can fetch it via the connection repository. From that you can ask from the ConnectionData and from that get the access token.

            Of course, if you have a connection, then why do you need an access token at all? FacebookTemplate can give you a RestTemplate that is already instrumented to include the token in the Authorization header for all requests. You don't need to create your own FacebookTemplate in this case...the connection framework can do it for you.

            If you're creating your own instance of FacebookTemplate, then you're going to need the token when you create it. There's no way to get an access token from FacebookTemplate itself.

            I could offer a handful of other thoughts on getting an access token, but without knowing whether or not your'e using Spring Social's connection framework, I'd just be speculating on what you should do.


            • #7
              Well I build up my game on canvas tutorial so using ProviderSignInController and do have a connection
              Thanks to you I get the access token in signIn method by connection.createData().getAccessToken();
              I own you a beer for ur immediate responses


              • #8
                Okay, so if your app is a canvas app, then you can use CanvasSignInController instead of ProviderSignInController. CanvasSignInController won't require the user to go through anything other than a typical Canvas-oriented authorization, but will result in the user being authenticated and authorizing the app. And you'll end up with a connection in the UserConnection table.

                Which means that if you need to perform *any* operation against the API, you can simply inject the Facebook bean into your controller and use it however you see fit. See the Spring Social Canvas sample for an example of how this is done: Specifically, note that in SocialConfig, the Facebook bean is request-scoped so that it is for the current user and it is injected into HomeController.

                Once you have the Facebook bean injected, you can use it to get an OAuth-ready RestOperations. In the sample, HomeController uses the Facebook object to fetch a list of the user's friends. But instead you can use it to fetch anything you want via the RestOperations:

                SomeObject o = facebook.restOperations().getForObject("", SomeObject.class);
                (Note that "SomeObject" and "SOME_OBJECT_ID" aren't real...intended as placeholders for whatever it is that you're fetching.)

                You could also use the methods from the GraphApi interface for a higher-level access to Facebook API:

                SomeObject o = facebook.fetchObject(SOME_OBJECT_ID, SomeObject.class);
                In either case, the request will automatically carry the Authorization header with the connection's access token. There's no need for you to deal with the access token directly.

                However, if you really must deal with the access token directly (although I can think of no reason why), then you can get it via the connection. Start by injecting the connection repository:

                public MyController(ConnectionRepository connectionRepo) { ... }
                Then use it to get the access token:

                Connection<Facebook> conn = connectionRepo.getPrimaryConnection(Facebook.class);
                ConnectionData connData = conn.createData();
                String accessToken = connData.getAccessToken();


                • #9
                  After quite a long time, I hit the question again. Here is the answer

                  To post a score for the currently connected user is;
                  MultiValueMap<String, String> mvm = new LinkedMultiValueMap<String, String>();
                  mvm.add("score", "10");
                  HttpHeaders httpHeaders = new HttpHeaders();
                  HttpEntity httpEntity = new HttpEntity(mvm, httpHeaders);
                  Boolean result = facebook.restOperations().postForObject(""+facebook.userOperations().getUserProfile().getId()+"/scores", httpEntity, Boolean.TYPE, mvm);
                  and to get score;
                  mvm = new LinkedMultiValueMap<String, String>();
                  HttpHeaders httpHeaders = new HttpHeaders();
                  HttpEntity  httpEntity = new HttpEntity(mvm, httpHeaders);
                  String resultList = facebook.restOperations().getForObject(""+YOUR_APP_ID+"/scores", String.class, mvm);