所以这似乎是人们看到的常见错误,但我不知道如何解决它!我正在尝试在 Eclipse 中编写一个简单的 Java EE 应用程序(使用 JBoss v7.1),并且遇到了似乎没有尽头的麻烦!我终于明白了似乎是最后的障碍;获取访问令牌。
我正在“手动”编程它;自己设置http帖子:
@WebServlet("/callback") //user has accepted the authentication, and the auth code is sent to this url
public class CallBackServlet extends HttpServlet {
//my vars
private static final String clientId = "123.apps.googleusercontent.com";
private static final String clientSecret = "123abc";
private static final String redirectUri = "http://localhost:8080/WebProjectA/callback";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String code = request.getParameter("code"); //grabs the auth code
//set up http post
HttpClient client = new HttpClient( );
String postUrl = "/o/oauth2/token";
HostConfiguration hf=new HostConfiguration();
hf.setHost("accounts.google.com"); //problems start here. Anything other than "accounts.google.com" results in an 'unknown host' exception
PostMethod postData = new PostMethod(postUrl);
postData.setHostConfiguration(hf);
//add post message parameters
postData.addParameter(URLEncoder.encode("code"), URLEncoder.encode(code));
postData.addParameter(URLEncoder.encode("client_id"), URLEncoder.encode(clientId));
postData.addParameter(URLEncoder.encode("client_secret"), URLEncoder.encode(clientSecret));
postData.addParameter(URLEncoder.encode("redirect_uri"), URLEncoder.encode(redirectUri));
postData.addParameter(URLEncoder.encode("grant_type"), URLEncoder.encode("authorization_code"));
client.executeMethod(postData);
String postResponseb = postData.getResponseBodyAsString( );
out.println(postResponseb); //print the info to the browser
postData.releaseConnection( );
}
}
重定向消息希望我使用“https://accounts.google.com”主机,但是当我将“accounts.google.com”以外的任何内容作为主机时,我得到一个未知主机异常。
提前感谢您的帮助!