我試圖通過這樣檢索Facebook用戶令牌。我知道它可以在Python中完成。獲取臉書用戶令牌
public string GetToken(string Username, string Password)
{
wb.DownloadData("https://login.facebook.com/login.php?login_attempt=1");
var data = new NameValueCollection();
data["email"] = UserName;
data["pass"] = Password;
byte[] responsebytes = wb.UploadValues("https://login.facebook.com/login.php?login_attempt=1", "POST", data);
string responsebody = Encoding.UTF8.GetString(wb.("https://www.facebook.com/dialog/oauth?client_id=282892925078054&redirect_uri=https://www.facebook.com/&response_type=token"));
Console.WriteLine(responsebody);
return responsebody;
}
但它沒有返回令牌。我知道這是可以做到的,因爲我已經在python中看到過它。 也有一個簡單的方法從這裏獲取用戶ID?
編輯:
這是它是如何在python完成:
def get_token(self, username, password):
self.session.get("https://login.facebook.com/login.php?login_attempt=1")
self.session.post("https://login.facebook.com/login.php?login_attempt=1", data={'email': username, 'pass': password})
return self.session.get("https://www.facebook.com/dialog/oauth?client_id=282892925078054&redirect_uri=https://www.facebook.com/&response_type=token").url.split("=")[1].split("&")[0]
好遠我來此。
//Login to facebook.
string formUrl = "https://login.facebook.com/login.php?login_attempt=1"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("email_address={0}&password={1}", email, pass);
string cookieHeader;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
Console.WriteLine(cookieHeader);
Console.WriteLine();
//Get token
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/dialog/oauth?client_id=282892925078054&redirect_uri=https://www.facebook.com/&response_type=token");
webRequest.Headers.Add("Cookie", cookieHeader);
//webRequest.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string redirectUrl = response.Headers.Get("Location");
Console.WriteLine(redirectUrl);
但它沒有給我正確的URL。我究竟做錯了什麼?
我希望這樣的事情:
https://www.facebook.com/#access_token=BAAEBSiRPCiYBAH0yF1cmZB14RBs9ZBvN7xQJaDvZAxd29WZCAZCpZAVzHXONNlUd9MOZAsTcSUimW7GITwrvN3px1XJSZBvK3wATdLzlQVOqQmlpBfs0ZCpsfydQV4ZCJEmpk4lmh9JCKbli78IDozYZBONxVszFZACQAgL2WPXF7680NGDQtD2IHl0oj6xbfqAtqpURSdJJmoZBXZAQZDZD&expires_in=5822
感謝