我試圖從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以編程方式登錄谷歌應用程序引擎C#
我知道該怎麼做。 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 protected]&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方法得到的,該工程。有誰能告訴我如何發佈數據?
謝謝。
編輯:
仍然沒有運氣。我嘗試了幾種方法,例如在app.yaml中更改爲[login:required],將[secure:always]添加到app.yaml並將請求協議更改爲https,將continue參數附加到/ _ah/login,但所有他們不工作:(
我完全不知道爲什麼POST不工作,但GET。任何想法?