我正在构建一个 Google 应用引擎 Java 应用程序,需要检索域的共享联系人列表。我首先尝试实现 Google Authsub 进行身份验证我编写代码
@SuppressWarnings("serial")
public class Contact_SearchServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
String nextUrl = "http://threeleggedoauth.appspot.com/authsub";
String scope = "http://www.google.com/m8/feeds/";
boolean secure = true; // set secure=true to request secure AuthSub tokens
boolean session = true;
String authSubUrl = AuthSubUtil.getRequestUrl(nextUrl, scope, secure, session);
resp.sendRedirect(authSubUrl);
}
}
当它将向该 servlet 发送请求时,我尝试获取会话令牌,然后尝试调用包含所有必要标头的url http://www.google.com/m8/feeds/contacts/iritesh.com/full我的代码这是映射到 /authsub 是
public class Authsub extends HttpServlet {
private static final Logger log = Logger.getLogger(Authsub.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String singleUseToken = AuthSubUtil.getTokenFromReply(req.getQueryString());
resp.getWriter().println(singleUseToken);
java.security.PrivateKey privateKey;
String sessionToken = null;
try {
privateKey = AuthSubUtil.getPrivateKeyFromKeystore("Example.jks", "changeme", "Example", "changeme");
sessionToken = AuthSubUtil.exchangeForSessionToken(singleUseToken, privateKey);
resp.getWriter().println("SessionToken is "+sessionToken);
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
resp.getWriter().println("Hello World1");
e.printStackTrace();
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
resp.getWriter().println("Hello World2"+e.getMessage()+e.getInternalReason()+e.getDebugInfo()+e.getLocalizedMessage());
e.printStackTrace();
}
URL uu=new URL("http://www.google.com/m8/feeds/contacts/iritesh.com/full");
HttpURLConnection cc= (HttpURLConnection) uu.openConnection();
cc.setRequestMethod("GET");
cc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
cc.setRequestProperty("Authorization" ,"AuthSub token=\""+sessionToken+"\"");
cc.setRequestProperty("GData-Version" ,"3.0" );
BufferedReader br = new BufferedReader(new InputStreamReader(cc.getInputStream()));
String sb = "";
String line;
while ((line = br.readLine()) != null) {
sb+= line+"\n";
}
br.close();
resp.getWriter().println("Response is "+sb);
}
}
但是我遇到了错误,当我在授权后重定向到threeleggedoauth.appspot.com/authsub 然后我遇到了错误401,这里有一个屏幕截图,有人可以指出我为什么会收到这个错误。我正在关注本教程https://developers.google.com/google-apps/domain-shared-contacts/并且错误的屏幕截图是您可以在threeleggedoauth.appspot.com 上检查实时错误,请帮助!