所以这似乎是人们看到的常见错误,但我无法解决如何解决它! 我正在尝试在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”以外的任何内容作为主机时,我会收到未知的主机异常。< / p>
提前感谢您的帮助!
答案 0 :(得分:1)
您可以在调用hf.setHost("accounts.google.com",-1,"HTTPS");
方法时传递协议。看到java文档。默认情况下,http。