All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.libs.OAuth2 Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package play.libs;

import java.util.HashMap;
import java.util.Map;

import play.mvc.Http.Request;
import play.mvc.Scope.Params;
import play.mvc.results.Redirect;

/**
 * Library to access ressources protected by OAuth 2.0. For OAuth 1.0a, see play.libs.OAuth.
 * See the facebook-oauth2 example for usage.
 *
 */
public class OAuth2 {

    public String authorizationURL;
    public String accessTokenURL;
    public String clientid;
    public String secret;
    public OAuth2(String authorizationURL,
            String accessTokenURL,
            String clientid,
            String secret) {
        this.accessTokenURL = accessTokenURL;
        this.authorizationURL = authorizationURL;
        this.clientid = clientid;
        this.secret = secret;
    }

    public static boolean isCodeResponse() {
        return Params.current().get("code") != null;
    }

    public void requestAccessToken() {
        String callbackURL = Request.current().getBase() + Request.current().url;
        throw new Redirect(accessTokenURL
                + "?client_id=" + clientid
                + "&redirect_uri=" + callbackURL);
    }

    public String getAccessToken() {
        String callbackURL = Request.current().getBase() + Request.current().url;
        String accessCode = Params.current().get("code");
        Map params = new HashMap();
        params.put("client_id", clientid);
        params.put("client_secret", secret);
        params.put("redirect_uri", callbackURL);
        params.put("code", accessCode);
        Map response = WS.url(authorizationURL).params(params).get().getQueryString();
        return response.get("access_token");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy