1
+ package com .baeldung .facebook ;
2
+
3
+ import com .restfb .BinaryAttachment ;
4
+ import com .restfb .Connection ;
5
+ import com .restfb .DefaultFacebookClient ;
6
+ import com .restfb .FacebookClient ;
7
+ import com .restfb .Parameter ;
8
+ import com .restfb .Version ;
9
+ import com .restfb .types .FacebookType ;
10
+ import com .restfb .types .Page ;
11
+ import com .restfb .types .User ;
12
+ import com .restfb .exception .FacebookOAuthException ;
13
+ import com .restfb .exception .FacebookResponseContentException ;
14
+
15
+ import org .springframework .beans .factory .annotation .Autowired ;
16
+ import org .springframework .beans .factory .annotation .Value ;
17
+ import org .springframework .stereotype .Service ;
18
+
19
+ import java .io .InputStream ;
20
+ import java .io .IOException ;
21
+ import java .util .List ;
22
+ import java .util .logging .Level ;
23
+ import java .util .logging .Logger ;
24
+
25
+ @ Service
26
+ public class FacebookService {
27
+
28
+ @ Autowired
29
+ private FacebookClient facebookClient ;
30
+
31
+ @ Value ("${facebook.app.secret}" )
32
+ private String appSecret ;
33
+
34
+ private static final Logger logger = Logger .getLogger (FacebookService .class .getName ());
35
+
36
+
37
+ public User getUserProfile () {
38
+ try {
39
+ return facebookClient .fetchObject ("me" , User .class , Parameter .with ("fields" , "id,name,email" ));
40
+ } catch (FacebookOAuthException e ) {
41
+ // Handle expired/invalid token
42
+ logger .log (Level .SEVERE ,"Authentication failed: " + e .getMessage ());
43
+ return null ;
44
+ } catch (FacebookResponseContentException e ) {
45
+ // General API errors
46
+ logger .log (Level .SEVERE ,"API error: " + e .getMessage ());
47
+ return null ;
48
+ }
49
+ }
50
+
51
+ public List <User > getFriendList () {
52
+ try {
53
+ Connection <User > friendsConnection = facebookClient .fetchConnection ("me/friends" , User .class );
54
+ return friendsConnection .getData ();
55
+ } catch (Exception e ) {
56
+
57
+ logger .log (Level .SEVERE ,"Error fetching friends list: " + e .getMessage ());
58
+ return null ;
59
+ }
60
+ }
61
+
62
+ public String postStatusUpdate (String message ) {
63
+ try {
64
+ FacebookType response = facebookClient .publish ("me/feed" , FacebookType .class , Parameter .with ("message" , message ));
65
+ return "Post ID: " + response .getId ();
66
+ } catch (Exception e ) {
67
+ logger .log (Level .SEVERE ,"Failed to post status: " + e .getMessage ());
68
+ return null ;
69
+ }
70
+ }
71
+
72
+ public void uploadPhotoToFeed () {
73
+ try (InputStream imageStream = getClass ().getResourceAsStream ("/static/image.jpg" )) {
74
+ FacebookType response = facebookClient .publish ("me/photos" , FacebookType .class , BinaryAttachment .with ("image.jpg" , imageStream ),
75
+ Parameter .with ("message" , "Uploaded with RestFB" ));
76
+ logger .log (Level .INFO ,"Photo uploaded. ID: " + response .getId ());
77
+ } catch (IOException e ) {
78
+ logger .log (Level .SEVERE ,"Failed to read image file: " + e .getMessage ());
79
+ }
80
+ }
81
+
82
+ public String postToPage (String pageId , String message ) {
83
+ try {
84
+ Page page = facebookClient .fetchObject (pageId , Page .class , Parameter .with ("fields" , "access_token" ));
85
+
86
+ FacebookClient pageClient = new DefaultFacebookClient (page .getAccessToken (), appSecret , Version .LATEST );
87
+
88
+ FacebookType response = pageClient .publish (pageId + "/feed" , FacebookType .class , Parameter .with ("message" , message ));
89
+
90
+ return "Page Post ID: " + response .getId ();
91
+ } catch (Exception e ) {
92
+ logger .log (Level .SEVERE ,"Failed to post to page: " + e .getMessage ());
93
+ return null ;
94
+ }
95
+ }
96
+ }
0 commit comments