Created
January 20, 2012 14:05
-
-
Save sody/1647506 to your computer and use it in GitHub Desktop.
Custom OAuth API with SpringSocial. Google OAuth2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Google extends ApiBinding { | |
String API_URL_BASE = "https://www.googleapis.com/oauth2/v2/"; | |
UserOperations userOperations(); | |
} | |
public interface UserOperations { | |
GoogleProfile getUserProfile(); | |
} | |
public class GoogleProfile implements Serializable { | |
private final String id; | |
private String name; | |
private String givenName; | |
private String familyName; | |
private String gender; | |
private Locale locale; | |
private String link; | |
private String pictureUrl; | |
public GoogleProfile(final String id) { | |
this.id = id; | |
} | |
public String getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getGivenName() { | |
return givenName; | |
} | |
public String getFamilyName() { | |
return familyName; | |
} | |
public String getGender() { | |
return gender; | |
} | |
public Locale getLocale() { | |
return locale; | |
} | |
public String getLink() { | |
return link; | |
} | |
public String getPictureUrl() { | |
return pictureUrl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GoogleModule extends SimpleModule { | |
public GoogleModule() { | |
super("GoogleModule", new Version(1, 0, 0, "alpha-1")); | |
} | |
@Override | |
public void setupModule(final SetupContext context) { | |
context.setMixInAnnotations(GoogleProfile.class, GoogleProfileMixin.class); | |
} | |
} | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
abstract class GoogleProfileMixin { | |
@JsonCreator | |
GoogleProfileMixin( | |
@JsonProperty("id") String id) { | |
} | |
@JsonProperty("name") | |
private String name; | |
@JsonProperty("given_name") | |
private String givenName; | |
@JsonProperty("family_name") | |
private String familyName; | |
@JsonProperty("gender") | |
private String gender; | |
@JsonProperty("locale") | |
private Locale locale; | |
@JsonProperty("link") | |
private String link; | |
@JsonProperty("picture") | |
private String pictureUrl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GoogleTemplate extends AbstractOAuth2ApiBinding implements Google { | |
private UserOperations userOperations; | |
public GoogleTemplate() { | |
initialize(); | |
} | |
public GoogleTemplate(final String accessToken) { | |
super(accessToken); | |
initialize(); | |
} | |
public UserOperations userOperations() { | |
return userOperations; | |
} | |
@Override | |
protected MappingJacksonHttpMessageConverter getJsonMessageConverter() { | |
final MappingJacksonHttpMessageConverter converter = super.getJsonMessageConverter(); | |
final ObjectMapper objectMapper = new ObjectMapper(); | |
objectMapper.registerModule(new GoogleModule()); | |
converter.setObjectMapper(objectMapper); | |
return converter; | |
} | |
private void initialize() { | |
userOperations = new UserTemplate(getRestTemplate(), isAuthorized()); | |
} | |
} | |
public class UserTemplate implements UserOperations { | |
private static final LinkedMultiValueMap<String, String> EMPTY_PARAMETERS = new LinkedMultiValueMap<String, String>(); | |
private final RestTemplate template; | |
private final boolean authorized; | |
public UserTemplate(final RestTemplate template, final boolean authorized) { | |
this.template = template; | |
this.authorized = authorized; | |
} | |
public GoogleProfile getUserProfile() { | |
requireAuthorization(); | |
return template.getForObject(buildUri("userinfo"), GoogleProfile.class); | |
} | |
protected void requireAuthorization() { | |
if (!authorized) { | |
throw new MissingAuthorizationException(); | |
} | |
} | |
protected URI buildUri(final String path) { | |
return buildUri(path, EMPTY_PARAMETERS); | |
} | |
protected URI buildUri(final String path, final String parameterName, final String parameterValue) { | |
final MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>(); | |
parameters.set(parameterName, parameterValue); | |
return buildUri(path, parameters); | |
} | |
protected URI buildUri(final String path, final MultiValueMap<String, String> parameters) { | |
return URIBuilder.fromUri(Google.API_URL_BASE + path).queryParams(parameters).build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GoogleServiceProvider extends AbstractOAuth2ServiceProvider<Google> { | |
public GoogleServiceProvider(final String clientId, final String clientSecret) { | |
super(new GoogleOAuth2Template(clientId, clientSecret)); | |
} | |
@Override | |
public Google getApi(final String accessToken) { | |
return new GoogleTemplate(accessToken); | |
} | |
} | |
public class GoogleOAuth2Template extends OAuth2Template { | |
private static final String AUTHORIZE_URL = "https://accounts.google.com/o/oauth2/auth"; | |
private static final String ACCESS_TOKEN_URL = "https://accounts.google.com/o/oauth2/token"; | |
public GoogleOAuth2Template(final String clientId, final String clientSecret) { | |
super(clientId, clientSecret, AUTHORIZE_URL, ACCESS_TOKEN_URL); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment