com.osomapps.pt.token.FacebookService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 58
dl 0
loc 67
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A FacebookService() 0 8 1
A getProfilePictureUrl(String) 0 18 3
A getProfileNameAndId(String) 0 24 3
1
package com.osomapps.pt.token;
2
3
import static java.time.temporal.ChronoUnit.YEARS;
4
5
import com.github.scribejava.apis.FacebookApi;
6
import com.github.scribejava.core.builder.ServiceBuilder;
7
import com.github.scribejava.core.model.OAuth2AccessToken;
8
import com.github.scribejava.core.model.OAuthRequest;
9
import com.github.scribejava.core.model.Response;
10
import com.github.scribejava.core.model.Verb;
11
import com.github.scribejava.core.oauth.OAuth20Service;
12
import com.osomapps.pt.UnauthorizedException;
13
import java.io.IOException;
14
import java.time.LocalDate;
15
import java.time.format.DateTimeFormatter;
16
import java.util.Map;
17
import java.util.Optional;
18
import lombok.extern.slf4j.Slf4j;
19
import org.springframework.stereotype.Service;
20
21
@Service
22
@Slf4j
23
class FacebookService {
24
    private static final String PICTURE_URL =
25
            "https://graph.facebook.com/me/picture?redirect=false&type=large";
26
    private static final String NAME_URL =
27
            "https://graph.facebook.com/me?fields=name,gender,birthday&locale=en_US";
28
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy");
29
    private OAuth20Service service;
30
    private OAuthRequest requestName;
31
    private OAuthRequest requestPicture;
32
33
    FacebookService() {
34
        service =
35
                new ServiceBuilder()
36
                        .apiKey("your_api_key")
37
                        .apiSecret("your_api_secret")
38
                        .build(FacebookApi.instance());
39
        requestName = new OAuthRequest(Verb.GET, NAME_URL, service);
40
        requestPicture = new OAuthRequest(Verb.GET, PICTURE_URL, service);
41
    }
42
43
    Optional<FacebookResponse> getProfileNameAndId(String accessTokenString) {
44
        try {
45
            final OAuth2AccessToken accessToken = new OAuth2AccessToken(accessTokenString);
46
            service.signRequest(accessToken, requestName);
47
            final Response response = requestName.send();
48
            if (!response.isSuccessful()) {
49
                throw new UnauthorizedException(
50
                        "Error while requesting data from facebook: " + response.getMessage());
51
            }
52
            org.springframework.boot.json.JsonParser springParser =
53
                    org.springframework.boot.json.JsonParserFactory.getJsonParser();
54
            Map<String, Object> object = springParser.parseMap(response.getBody());
55
            LocalDate birthday = LocalDate.parse(String.valueOf(object.get("birthday")), FORMATTER);
56
            final FacebookResponse facebookResponse =
57
                    new FacebookResponse(
58
                            String.valueOf(object.get("id")),
59
                            String.valueOf(object.get("name")),
60
                            String.valueOf(object.get("gender")),
61
                            birthday,
62
                            YEARS.between(birthday, LocalDate.now()));
63
            return Optional.of(facebookResponse);
64
        } catch (IOException ex) {
65
            log.error(ex.getMessage(), ex);
66
            return Optional.empty();
67
        }
68
    }
69
70
    @SuppressWarnings("unchecked")
71
    Optional<String> getProfilePictureUrl(String accessTokenString) {
72
        try {
73
            final OAuth2AccessToken accessToken = new OAuth2AccessToken(accessTokenString);
74
            service.signRequest(accessToken, requestPicture);
75
            final Response response = requestPicture.send();
76
            if (!response.isSuccessful()) {
77
                throw new UnauthorizedException(response.getMessage());
78
            }
79
            final String pictureBody = response.getBody();
80
            org.springframework.boot.json.JsonParser springParser =
81
                    org.springframework.boot.json.JsonParserFactory.getJsonParser();
82
            Map<String, Object> object = springParser.parseMap(pictureBody);
83
            return Optional.ofNullable(
84
                    String.valueOf(((Map<String, Object>) object.get("data")).get("url")));
85
        } catch (IOException ex) {
86
            log.error(ex.getMessage(), ex);
87
            return Optional.empty();
88
        }
89
    }
90
}
91