使用Google的scribejava库进行隐式代码授予

时间:2018-10-04 13:15:34

标签: scribe

我能够通过授权码授予流程获取访问令牌(对于Google oauth2.0)。我在scribejava的官方github页面上进行了很多搜索,但是找不到通过隐式代码授予流程执行相同操作的方法。

这是授权码授予流程的代码

@WebServlet("/home")
public class Home extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        final String cliendId = "client_id";
        final String clientSecret = "clinet_secret";
        final String redirectUri = "http://127.0.0.1:8080/OAuth/redirect";
        final String scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.login";
        final String secretState = Double.toString(Math.random());

        //Construct Google authorization URL
        OAuth20Service authUrl = new ServiceBuilder().apiKey(cliendId).apiSecret(clientSecret).debug().scope(scope)
                .state(secretState).callback(redirectUri).build(GoogleApi20.instance());

        // Add additional parameters
        final Map<String, String> additionalParams = new HashMap<>();
        //additionalParams.put("access_type", "offline");
        additionalParams.put("prompt", "consent");

        // Obtain authorization URL
        String authorizationUrl = authUrl.getAuthorizationUrl(additionalParams);
        System.out.println("Authorization Url - "+authorizationUrl + "\n");

        // Add authUrl to session variable to be used in Redirect.java
        HttpSession session = request.getSession();
        session.setAttribute("authUrl", authUrl);
        response.sendRedirect(authorizationUrl);
    }
}

0 个答案:

没有答案