2013-04-13 89 views
6

Tôi đang cố truy xuất mã thông báo người dùng facebook bằng cách thực hiện việc này. Tôi biết nó có thể được thực hiện trong python.Nhận mã thông báo người dùng facebook

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; 
    } 

Nhưng không trả lại mã thông báo. Tôi biết nó có thể được thực hiện, bởi vì tôi đã thấy điều này trong python. Cũng có cách dễ dàng để có được userid từ đây?

chỉnh sửa:

Đây là cách nó được thực hiện trong 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] 

Ok tôi đã đi xa này.

 //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); 

Nhưng nó không cho tôi URL đúng. Tôi đang làm gì sai?

chỉnh sửa: Tôi đã thử với AllowautoRedirect true/false. Với sai nó trả về URL này. https://www.facebook.com/login.php?api_key=282892925078054&skip_api_login=1&display=page&cancel_url=https%3A%2F%2Fwww.facebook.com%2F%3Ferror_reason%3Duser_denied%26error%3Daccess_denied%26error_description%3DThe%2Buser%2Bdenied%2Byour%2Brequest.&fbconnect=1&next=https%3A%2F%2Fwww.facebook.com%2Fdialog%2Fpermissions.request%3F_path%3Dpermissions.request%26app_id%3D282892925078054%26client_id%3D282892925078054%26redirect_uri%3Dhttps%253A%252F%252Fwww.facebook.com%252F%26display%3Dpage%26response_type%3Dtoken%26fbconnect%3D1%26from_login%3D1&rcount=1

tôi mong đợi một cái gì đó như thế này:

https://www.facebook.com/#access_token=BAAEBSiRPCiYBAH0yF1cmZB14RBs9ZBvN7xQJaDvZAxd29WZCAZCpZAVzHXONNlUd9MOZAsTcSUimW7GITwrvN3px1XJSZBvK3wATdLzlQVOqQmlpBfs0ZCpsfydQV4ZCJEmpk4lmh9JCKbli78IDozYZBONxVszFZACQAgL2WPXF7680NGDQtD2IHl0oj6xbfqAtqpURSdJJmoZBXZAQZDZD&expires_in=5822 

Cảm ơn

Trả lời

0
CallbackManager callbackManager; 



protected void onCreate(Bundle savedInstanceState) 
{ 



FacebookSdk.sdkInitialize(getApplicationContext()); 



    callbackManager = CallbackManager.Factory.create(); 



    facebookLogin = (LoginButton) findViewById(R.id.login_button); 


    facebookLogin.setReadPermissions("user_friends"); 



    LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() 



{ 



      @Override 

      public void onSuccess(LoginResult loginResult) { 

       Log.d("Access Token" ,loginResult.getAccessToken().getToken()); 

       //here you get you access token 

      } 

      @Override 

      public void onCancel() { 

       Log.d("Cancel" ,"On cancel"); 

      } 

      @Override 

      public void onError(FacebookException e) 
{ 
       Log.d("Error","Error"); 

      } 
     }); 


    //Must set this on activity result 

    @Override 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 

     super.onActivityResult(requestCode, resultCode, data); 

     callbackManager.onActivityResult(requestCode, resultCode, data); 

    } 
Các vấn đề liên quan