以编程方式登录谷歌应用引擎c#

时间:2011-05-18 01:37:04

标签: c# google-app-engine django-nonrel

我试图从ASP.NET登录我的谷歌应用引擎应用程序几天,但没有运气。我已阅读以下文章并获得了基本想法。但没有什么对我有用 http://code.activestate.com/recipes/577217-routines-for-programmatically-authenticating-with-/ http://dalelane.co.uk/blog/?p=303
http://dalelane.co.uk/blog/?p=894
http://krasserm.blogspot.com/2010/01/accessing-security-enabled-google-app.html http://blog.notdot.net/2010/05/Authenticating-against-App-Engine-from-an-Android-app

我知道该怎么做。 1)从ClientLogin获取身份验证令牌。 2)从Google App Engine获取Cookie。 3)使用cookie将数据发布到我的应用程序(是的,我想发布数据,而不是在第二部分之后重定向)。但第三部分对我来说根本不起作用。它给我403错误。这是我的代码:

void PostToGAE()
{
    var auth = GetAuth();  // I can get the authtoken
    var cookies = GetCookies(auth);  // I can get the ACSID cookie

    var url = string.Format("http://test.appspot.com/do/something/");
    var content = "testvalue=test";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.KeepAlive = false;
    request.CookieContainer = cookies;
    byte[] byteArray = Encoding.UTF8.GetBytes(content);
    request.ContentLength = byteArray.Length;
    request.ContentType = "application/x-www-form-urlencoded";
    request.Method = "POST";
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // This gives me 403
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream);
    string result = reader.ReadToEnd();
    reader.Close();
}

CookieContainer GetCookies(string auth)
{
    CookieContainer cookies = new CookieContainer();
    var url = string.Format("http://test.appspot.com/_ah/login?auth={0}",
                            System.Web.HttpUtility.UrlEncode(auth));
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.AllowAutoRedirect = false;
    request.CookieContainer = cookies;
    request.ContentType = "application/x-www-form-urlencoded";
    request.Method = "GET";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream);
    string result = reader.ReadToEnd(); 
    reader.Close();
    return cookies;
}

string GetAuth()
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/accounts/ClientLogin");
    var content = "Email=test@gmail.com&Passwd=testpass&service=ah&accountType=HOSTED_OR_GOOGLE";
    byte[] byteArray = Encoding.UTF8.GetBytes(content);
    request.ContentLength = byteArray.Length;
    request.ContentType = "application/x-www-form-urlencoded";
    request.Method = "POST";
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream);
    string loginStuff = reader.ReadToEnd();
    reader.Close();

    var auth = loginStuff.Substring(loginStuff.IndexOf("Auth")).Replace("Auth=", "").TrimEnd('\n');
    return auth;
}

我的app.yaml看起来像这样:

- url: /do/something/
  script: something.py
  login: admin

如果我将方法POST改为GET,那就行了。谁能告诉我如何发布数据呢? 感谢。

EDITED


仍然没有运气。我尝试了几种方法,例如在app.yaml中更改为[login:required],将[secure:always]添加到app.yaml并将请求协议更改为https,将continue参数追加到/ _ah / login,但是所有人都不工作:(
我完全不知道为什么POST除了GET之外根本不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我做到了。我走错了路。这不是app引擎的问题,而是Django。我在谷歌应用引擎上使用Django-nonrel,我完全忘了将 @csrf_exempt 装饰器放到我的处理程序中。我之前遇到过同样的问题,但又一次。无论如何,上面的代码从一开始就显然正常工作。多么聪明的男孩:)