Question

Here I want to get all contacts mail id of a person. This code is redirecting to google site and after getting token and varifier it is returning status code and body which is printing on console. I want to display response.getBody()(all maial contacts of a person) in jsp page. How to do this?

public class googleOaoth extends ActionSupport {

private static final String NETWORK_NAME = "Google";
private static final String AUTHORIZE_URL = "https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=";
private static final String PROTECTED_RESOURCE_URL = "https://www.google.com/m8/feeds/contacts/default/full";
private static final String SCOPE = "https://www.google.com/m8/feeds";
OAuthService service = new ServiceBuilder().provider(GoogleApi.class).apiKey("www.mysite").apiSecret("****").scope(SCOPE).callback("http://mysite/mypage").build();
Token requestToken;
private String url;
private String oauth_verifier;
private String oauth_token;

public String getOauth_verifier() {
    return oauth_verifier;
}

public Token getRequestToken() {
    return requestToken;
}

public void setRequestToken(Token requestToken) {
    this.requestToken = requestToken;
}

public void setOauth_verifier(String oauth_verifier) {
    this.oauth_verifier = oauth_verifier;
}
Map session = ActionContext.getContext().getSession();
 //method which redirect to google page
public String LoginToGoogle() {



    System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
    System.out.println();

    // Obtain the Request Token
    System.out.println("Fetching the Request Token...");
    setRequestToken(service.getRequestToken());
    System.out.println("Got the Request Token!");
    System.out.println("(if your curious it looks like this: " + getRequestToken() + " )");
    System.out.println();
    session.put("googleAccessToken", getRequestToken());
    System.out.println("Now go and authorize Scribe here:");
    setUrl(AUTHORIZE_URL + getRequestToken().getToken());
    System.out.println(AUTHORIZE_URL + getRequestToken().getToken());

    return "redirect";
}
//after getting varification code this method will show body and res_code
public String acceptToken() {
    System.out.println("And paste the verifier here");
    System.out.print(">>");
    Verifier verifier = new Verifier(getOauth_verifier());
    System.out.println();

    // Trade the Request Token and Verfier for the Access Token
    System.out.println("Trading the Request Token for an Access Token...");
    //System.out.println("varifier : " + getOauth_verifier());
    // System.out.println("token  :" + getOauth_token());
    //requestToken = service.getRequestToken();
    setRequestToken((Token) session.get("googleAccessToken"));
    System.out.println("token  " + getRequestToken());
    Token accessToken = service.getAccessToken(getRequestToken(), verifier);//requestToken

    System.out.println("Got the Access Token!");
    System.out.println("(if your curious it looks like this: " + accessToken + " )");
    System.out.println();

    // Now let's go and ask for a protected resource!
    System.out.println("Now we're going to access a protected resource...");
    OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
    service.signRequest(accessToken, request);
    request.addHeader("GData-Version", "3.0");
    Response response = request.send();
    System.out.println("Got it! Lets see what we found...");
    System.out.println();
    System.out.println(response.getCode());
    System.out.println(response.getBody());

    System.out.println();
    System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
    return SUCCESS;
}
Was it helpful?

Solution

Inside your action method create variable (e.g. respBody) with public getters/setters and assign response body to it.

respBody = response.getBody();

Then in JSP you can show it using <s:property> tag.

<s:property value="respBody"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top