Android: How to get friends birth dates from facebook calendar using Graph API

后端 未结 2 615
太阳男子
太阳男子 2020-12-19 07:45

I\'m working on application which need birth dates of all friends. Can some body share code how to get birth dates of all friends using graph API. I\'m using following code:

2条回答
  •  爱一瞬间的悲伤
    2020-12-19 08:04

    First define your callback, where you'll get friends and birthday info, if authenticated:

    Session.StatusCallback statusCallback =  new Session.StatusCallback() {
    
      // callback when session changes state
      @Override
      public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            // Private method, to be defined
            makeFriendsRequest();
        }
      }
    
    };
    

    Then, you can open the session and pass in the necessary, "friends_birthday" permissions request:

    Session session = new Session(this);
    session.openForRead(new Session.OpenRequest(this)
                           .setCallback(statusCallback)
                           .setPermissions(Arrays.asList("friends_birthday")));
    

    Finally, here's the post-authentication method you can use to get friends info, including the birthday:

    private void makeFriendsRequest() {
        Request myFriendsRequest = Request.newMyFriendsRequest(Session.getActiveSession(), 
                new Request.GraphUserListCallback() {
    
            @Override
            public void onCompleted(List users, Response response) {
                if (response.getError() == null) {
                    // Handle response
                }
    
            }
    
        });
        // Add birthday to the list of info to get.
        Bundle requestParams = myFriendsRequest.getParameters();
        requestParams.putString("fields", "name,birthday");
        myFriendsRequest.setParameters(requestParams);
        myFriendsRequest.executeAsync();
    }
    

    Also check out the SessionLoginSample app, bundled with the SDK if you're looking for different ways you can implement Facebook Login.

提交回复
热议问题