aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/FacebookPageManager.java
blob: e9ed0e592186efa35d214ad59bead1ea012a68a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.juick.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

@Component
public class FacebookPageManager {
    private static Logger logger = LoggerFactory.getLogger(FacebookPageManager.class);
    @Value("${facebook_page_id:12345678}")
    private String pageId;
    @Value("${fb_page_access_token:12345678}")
    private String accessToken;

    public void post(String status, String link) {
        UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://graph.facebook.com/{page_id}/feed")
                .queryParam("message", status)
                .queryParam("link", link)
                .queryParam("access_token", accessToken)
                .buildAndExpand(pageId);
        RestTemplate api = new RestTemplate();
        ResponseEntity<String> response = api.exchange(uriComponents.toUri(),
                HttpMethod.POST, null, String.class);
        logger.info("Facebook response: {}", response.getBody());
    }
}