首页 > 解决方案 > 如何从重定向响应中获取授权码

问题描述

我正在为 Spotify 使用 Java 包装器,并且我正在尝试找到一种从重定向 URI 获取授权代码的方法,以便在请求访问令牌时可以使用它。我是 OAuth 的新手,所以任何帮助都会很棒!谢谢!

我能够从浏览器重定向复制代码并将其硬编码到我的代码中。

标签: javaoauth-2.0spotify

解决方案


您需要将 redirect_uri 参数设置为 Servlet url。

@WebServlet("your/servlet/Url")
public class MyServlet extends HttpServlet
{

    private static final long serialVersionUID = 1L;

    public MyServlet()
    {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        //get the authorization code 
        String code = request.getParameter("code");
        //get tokens and use them as per your requirement using code 
        ...
        //redirect user to the final destination
        response.sendRedirect("url");
    }
}

推荐阅读