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

  • How to use scores api

    I want to user facebook scores api ( https://developers.facebook.com/docs/howtos/scores/ ) 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.

    Comment


    • #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?

      Comment


      • #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.

        Comment


        • #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?

          Comment


          • #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.

            Comment


            • #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

              Comment


              • #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: https://github.com/SpringSource/spri...-social-canvas. 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:

                Code:
                SomeObject o = facebook.restOperations().getForObject("https://graph.facebook.com/SOME_OBJECT_ID", 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:

                Code:
                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:

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

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

                Comment


                • #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;
                  Code:
                            
                  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("https://graph.facebook.com/"+facebook.userOperations().getUserProfile().getId()+"/scores", httpEntity, Boolean.TYPE, mvm);
                  and to get score;
                  Code:
                  mvm = new LinkedMultiValueMap<String, String>();
                  HttpHeaders httpHeaders = new HttpHeaders();
                  HttpEntity  httpEntity = new HttpEntity(mvm, httpHeaders);
                  String resultList = facebook.restOperations().getForObject("https://graph.facebook.com/"+YOUR_APP_ID+"/scores", String.class, mvm);

                  Comment

                  Working...
                  X