问题描述:

i have downloaded a oauth spring project from here. this is a oauth example with google api. it has a class for api called UserResource.java.

content of UserResource.java is:

package com.rst.oauth2.google.api;

import static com.google.common.collect.Lists.newArrayList;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.rst.oauth2.google.security.GoogleProfile;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;

import org.springframework.security.oauth2.client.OAuth2ClientContext;

import org.springframework.security.oauth2.client.OAuth2RestOperations;

import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;

import org.springframework.stereotype.Component;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Component

@RequestMapping("find")

public class UserResource {

private static Logger LOGGER = LoggerFactory.getLogger(UserResource.class);

private static ObjectMapper OM = new ObjectMapper();

@Autowired private OAuth2RestOperations oauth2RestTemplate;

@RequestMapping(method = RequestMethod.GET)

public @ResponseBody List<String> findUsersStartingWithPrefix(@RequestParam("term") String usernamePrefix) throws JsonProcessingException {

LOGGER.info("Searching for users starting with {}", usernamePrefix);

List<String> list = newArrayList("[email protected]", "[email protected]");

GoogleProfile profile = getGoogleProfile();

LOGGER.info("Google Profile Data {}", OM.writerWithDefaultPrettyPrinter().writeValueAsString(profile));

list.add(profile.getEmail());

LOGGER.info("Returning users {}", list);

return list;

}

private GoogleProfile getGoogleProfile() {

String url = "https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + oauth2RestTemplate.getAccessToken();

ResponseEntity<GoogleProfile> forEntity = oauth2RestTemplate.getForEntity(url, GoogleProfile.class);

return forEntity.getBody();

}

}

and application.properties is:

# Embedded jetty server port

server.port=9000

google.client.id=306673655858-vn86n6s6jj2a6gtnjm881jongtb0qe4g.apps.googleusercontent.com

google.client.secret=J1PtoeX4GZKj9rHM3hiKJ6gF

google.auth.scope=https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/userinfo.profile,https://www.googleapis.com/auth/plus.login

google.accessTokenUri=https://accounts.google.com/o/oauth2/token

google.userAuthorizationUri=https://accounts.google.com/o/oauth2/auth

google.authorization.code=authorization_code

google.preestablished.redirect.url=http://localhost:9000/googleLogin

and GoogleProfile.java is:

package com.rst.oauth2.google.security;

import com.fasterxml.jackson.annotation.JsonProperty;

public class GoogleProfile {

private String id;

private String email;

@JsonProperty("verified_email")

private Boolean verifiedEmail;

private String name;

@JsonProperty("given_name")

private String givenName;

@JsonProperty("family_name")

private String familyName;

private String link;

private String picture;

private String gender;

private String locale;

private String hd;

public String getId() {

return id;

}

public String getEmail() {

return email;

}

public Boolean getVerifiedEmail() {

return verifiedEmail;

}

public String getName() {

return name;

}

public String getGivenName() {

return givenName;

}

public String getFamilyName() {

return familyName;

}

public String getLink() {

return link;

}

public String getPicture() {

return picture;

}

public String getGender() {

return gender;

}

public String getLocale() {

return locale;

}

public String getHd() {

return hd;

}

}

when i added more variables to GoogleProfile.java i got null value in these variables.

how can i get more data from google account?

相关阅读:
Top