如何以编程方式验证谷歌应用引擎(使用Java)?

时间:2011-06-15 12:20:50

标签: google-app-engine

我正在尝试以编程方式对Google App引擎进行身份验证。 我已经尝试过“gae-app-manager”项目中的代码示例,但它失败了:

tmp>java -jar net.sf.gae-app-manager-0.0.1-jar-with-dependencies.jar myaccount@gmail.com mypassword appname
Exception in thread "main" java.lang.Exception: Did not find ACSID cookie
        at net.sf.gaeappmanager.google.LogonHelper.loginToGoogleAppEngine(LogonHelper.java:85)
        at net.sf.gaeappmanager.google.appengine.Manager.retrieveAppQuotaDetails(Manager.java:34)
        at net.sf.gaeappmanager.google.appengine.Main.main(Main.java:55)

有什么想法吗?我能够获得令牌,但没有cookie。 代码(取自gae-app-manager项目 - http://gae-app-manager.git.sourceforge.net/git/gitweb.cgi?p=gae-app-manager/gae-app-manager;a=blob;f=src/main/java/net/sf/gaeappmanager/google/LogonHelper.java;h=8e09a6d7f864c29b10847ac7fd2eeab2d3e561e6;hb=HEAD):

 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  52                         nvps.add(new BasicNameValuePair("accountType", "HOSTED_OR_GOOGLE"));
  53                         nvps.add(new BasicNameValuePair("Email", userid));
  54                         nvps.add(new BasicNameValuePair("Passwd", password));
  55                         nvps.add(new BasicNameValuePair("service", "ah"));
  56                         nvps.add(new BasicNameValuePair("source", source));
  57 
  58                         HttpPost post = new HttpPost(
  59                                         "https://www.google.com/accounts/ClientLogin");
  60                         post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  61 
  62                         HttpResponse response = client.execute(post);
  63 
  64                         if (response.getStatusLine().getStatusCode() != 200) {
  65                                 throw new Exception("Error obtaining ACSID");
  66                         }
  67 
  68                         String authToken = getAuthToken(response.getEntity().getContent());
  69 
  70                         post.abort();
  71 
  72                         HttpGet get = new HttpGet(
  73                                         "https://appengine.google.com/_ah/login?auth=" + authToken);
  74 
  75                         response = client.execute(get);
  76 
  77                         for (Cookie cookie : client.getCookieStore().getCookies()) {
  78                                 if (cookie.getName().startsWith("ACSID")) {
  79                                         return cookie.getValue();
  80                                 }
  81                         }
  82 
  83                         get.abort();
  84 
  85                         throw new Exception("Did not find ACSID cookie");

谢谢,

2 个答案:

答案 0 :(得分:1)

您是否考虑过使用OAuth support而不是尝试以网络客户端身份登录?每个App Engine应用程序都可以充当OAuth提供程序,只需要很少的工作就可以在服务器端进行设置。

答案 1 :(得分:0)

要解决此问题,请使用“SACSID”代替“ACSID”