2009-11-25 24 views
0

我需要能夠使用帳戶用戶名和密碼將用戶登錄到我們的flickr帳戶。使用HttpWebRequest登錄到flickr的示例代碼(C#)

我一直在網上搜索了很長一段時間,但只發現了一個實現的點點滴滴。我根本沒有經驗過Http調用。我需要一個完整的例子。這是我的代碼至今:

HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest; 
http.Method = "POST"; 
http.ContentType = "application/x-www-form-urlencoded"; 
string postData = "FormNameForUserId=" + username + "&FormNameForPassword=" + password; 
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData); 
http.ContentLength = dataBytes.Length; 

using (Stream postStream = http.GetRequestStream()) 
{ 
    postStream.Write(dataBytes, 0, dataBytes.Length); 
} 

HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse; 

我在這一點上我想是搞清楚什麼所有參數都Flickr的主要問題,需要在「您」登錄

任何建議,歡迎

回答

1

你可以嘗試使用像Fiddler代理來檢查你的瀏覽器在登錄請求發送。

雖然最好的方法,但很可能會使用Flickr API代替。 FlickNet是API的.Net包裝器。

1

確保你設置了http.AllowAutoRedirect = false;這是2小時頭撞我的來源。有時,一旦您登錄後,發佈請求會返回到主頁的重定向..NET自動遵循重定向,但不會提交新獲取的Cookie。 >。 <

1

Flickr的API要求:

API Key 
Perms (Permissions: read, write, delete) 
Frob 
API Signature 

你的網址會這樣看:

http://flickr.com/services/auth/?api_key=[api_key]&perms=[perms]&frob=[frob]&api_sig=[api_sig] 

建立你FROB最簡單的方法和令牌是Flickr.Net。這裏有一些代碼是這樣的:

Flickr ourFlickr = new Flickr(); 

ourFlickr.ApiSecret = ApiSecret; 
ourFlickr.ApiKey = ApiKey; 

string signature = ApiSecret + "api_key" + ApiKey + "methodflickr.auth.getFrob"; 

string frob = ourFlickr.AuthGetFrob().ToString(); 

string url = "http://flickr.com/services/auth/?api_key=" + ourFlickr.ApiKey + "&perms=" + "read" + " &frob=" + frob + "&api_sig=" + signature; 

我希望這會有所幫助。無論如何,使用他們的API和接口將比反向設計他們的Web表單容易得多。