尝试获取共享联系人时出现UnExpected 401错误

时间:2013-03-31 15:43:56

标签: google-app-engine google-contacts

我正在构建一个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/并且错误的屏幕截图是here你可以在threeleggedoauth.appspot.com查看实时错误,请帮助!!

0 个答案:

没有答案