0

轉到下面來看看回答Facebook的訪問令牌 - 自動獲取令牌

我得到Facebook的訪問令牌罰款,但如果我有麻煩時,我想這個過程自動化。

如果我訪問這個URL是瀏覽器,我得到訪問令牌就好了。

例子:

我粘貼到瀏覽器並回車這一點。

https://www.facebook.com/dialog/oauth?client_id=324234343434&scope=['ads_read', 'ads_management']&redirect_uri=http://www.kb-demos.com/login_success.html&response_type=token

接着,我會發送到這個頁面:

http://www.kb-demos.com/login_success.html?#access_token=34543534534534KJ534LKJLKJLKHLH4534534J5KH345KJ3L4H53KJ5H3K4LJH34KH54K&expires_in=5180653

我改變了訪問令牌一塊所以它不是一個真正的令牌

中提琴訪問令牌!

我想要做的是複製相同的行爲與代碼。我越來越接近但並不完全在那裏。

我不斷收到user_denied錯誤。

%3Ferror%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duser_denied%23_%3D_ &顯示=頁&語言環境= EN_US & logger_id = 786a3153-1d81-415c-8dca-f8fa8b0cd630

我將所有標題輸出到控制檯。這是我所關注的位置標題**

我認爲它與302重定向有關嗎?

ApplicationId = request.ClientId; 
string permissions = "['ads_management', 'ads_read']"; 

var destinationURL = String.Format(
@"https://www.facebook.com/dialog/oauth?client_id={0}&scope={1}&redirect_uri=http://www.kb-demos.com/login_success.html&response_type=token", 
ApplicationId, 
permissions); 

// Create a new 'HttpWebRequest' Object to the mentioned URL. 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(destinationURL); 
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"; 

myHttpWebRequest.AllowAutoRedirect = false; 
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable. 
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 

//Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}", myHttpWebRequest.Headers); // my http headers 
// Print the HTML contents of the page to the console. 


var headers = myHttpWebResponse.Headers; 

// output all the headers 
foreach(var header in headers) { 
    Console.WriteLine(header.ToString() + ": " + headers[header.ToString()] + "\n"); 

} 

#region 
//Stream streamResponse = myHttpWebResponse.GetResponseStream(); 
//StreamReader streamRead = new StreamReader(streamResponse); 
//Char[] readBuff = new Char[256]; 
//int count = streamRead.Read(readBuff, 0, 256); 
//Console.WriteLine("\nThe HTML contents of page the are : \n\n "); 
//while (count > 0) 
//{ 
// String outputData = new String(readBuff, 0, count); 
// Console.Write(outputData); 
// count = streamRead.Read(readBuff, 0, 256); 
//} 
//// Close the Stream object. 
//streamResponse.Close(); 
//streamRead.Close(); 
// Release the HttpWebResponse Resource. 
#endregion 


myHttpWebResponse.Close(); 

Console.ReadLine(); 

我得到一個user_denied錯誤在這裏。但在瀏覽器中,我獲得了一個完美的令牌。我無法弄清楚爲什麼。

enter image description here

中的位置標頭中的標頭似乎工作使用瀏覽器的時候。

可能的情況下,如果我不能得到上述工作: 我想知道是否有與API的瀏覽器?什麼事情我可以從命令行調用 - 傳遞一些參數 - 然後得到重定向URL解析變量?

+0

你不應該爲「自動」這一點。刮臉Facebook頁面,並使用他們網站上的任何自動化工具,明確反對他們的服務承諾。 – CBroe

+0

@Croro lmao。我不認爲你明白這個應用程序的目的是什麼。我擁有允許訪問此應用的帳戶。我只是在計劃的作業上運行此應用程序以獲得每日訪問令牌,因此我可以運行另一個我開發的應用程序,它可以爲許多客戶端每天獲取SEO報告,而不是通過前端爲每個客戶端手動登錄以下載這些報告。 – Radmation

+0

@CBroe問題在於訪問令牌每60天過期一次。所以現在我已經實現了獲取訪問令牌的自動檢索,所以我不必再觸摸它了。加上API的一個主要原因是自動完成任務。我不是「刮」網站,我只是給一個應用程序訪問我的Facebook帳戶.. – Radmation

回答

-1

此代碼將自動執行的的access_token檢索。您必須擁有要申請訪問令牌的帳戶的憑據。

更新

第一次登錄到Facebook帳戶。

 // LOG INTO FACEBOOK ACCT 
     string email = "[email protected]"; 
     string pw = "yourPassWord"; 

     CookieContainer cookieJar = new CookieContainer(); 

     HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("https://www.facebook.com"); 
     request1.CookieContainer = cookieJar; 

     request1.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"; 

     //Get the response from the server and save the cookies from the first request.. 
     HttpWebResponse response = (HttpWebResponse)request1.GetResponse(); 
     var cookies = response.Cookies; 
     cookieJar.Add(cookies); 
     response.Close();// close the response 


     string getUrl = "https://www.facebook.com/login.php?login_attempt=1"; 

     string postData = String.Format("email={0}&pass={1}", email, pw); 
     HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl); 
     getRequest.CookieContainer = cookieJar; 
     //Adding Previously Received Cookies 
     getRequest.CookieContainer.Add(cookies); 
     getRequest.Method = WebRequestMethods.Http.Post; 
     getRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"; 
     getRequest.AllowWriteStreamBuffering = true; 
     getRequest.ProtocolVersion = HttpVersion.Version11; 
     getRequest.AllowAutoRedirect = true; 
     getRequest.ContentType = "application/x-www-form-urlencoded"; 

     byte[] byteArray = Encoding.ASCII.GetBytes(postData); 
     getRequest.ContentLength = byteArray.Length; 
     Stream newStream = getRequest.GetRequestStream(); //open connection 
     newStream.Write(byteArray, 0, byteArray.Length); // Send the data. 
     newStream.Close(); 

     HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse(); 
     using (StreamReader sr = new StreamReader(getResponse.GetResponseStream())) 
     { 
      string sourceCode = sr.ReadToEnd(); 
     } 

然後請求的access_token

   ApplicationId = request.ClientId; // your application id 
       string permissions = "['ads_management', 'ads_read']"; 

       var destinationURL = String.Format(
       @"https://www.facebook.com/dialog/oauth?client_id={0}&scope={1}&redirect_uri=http://www.kb-demos.com/login_success.html&response_type=token", 
       ApplicationId, 
       permissions); 

       // Create a new 'HttpWebRequest' Object to the mentioned URL. 
       HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(destinationURL); 
       // use the same cookie container and cookies 
       myHttpWebRequest.CookieContainer = cookieJar; 
       myHttpWebRequest.CookieContainer.Add(cookies); //recover cookies First request 

       myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"; 

       myHttpWebRequest.AllowAutoRedirect = false; 
       // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable. 
       HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 

       Console.WriteLine("\nThe Request HttpHeaders are \n\n\tName\t\tValue\n{0}", myHttpWebRequest.Headers); // my http headers 
       Console.WriteLine("\nThe Request HttpHeaders are \n\n\tName\t\tValue\n{0}", myHttpWebRequest.CookieContainer); // my http headers 
       //Console.WriteLine("\nThe Request HttpHeaders are \n\n\tName\t\tValue\n{0}", cookies); // my http headers 

       var headers = myHttpWebResponse.Headers; 
       // output all the headers 
       foreach (var header in headers) 
       { 
        Console.WriteLine(header.ToString() + ": " + headers[header.ToString()] + "\n"); 

       } 

       var cow = GetParams(headers["Location"]); 
       string accessToken = ""; 
       accessToken = cow["#access_token"]; 

和輔助方法

/// <summary> 
    /// Helper method to get Params from URL using RegEx 
    /// </summary> 
    static Dictionary<string, string> GetParams(string uri) 
    { 
     var matches = Regex.Matches(uri, @"[\?&](([^&=]+)=([^&=#]*))", RegexOptions.Compiled); 
     return matches.Cast<Match>().ToDictionary(
      m => Uri.UnescapeDataString(m.Groups[2].Value), 
      m => Uri.UnescapeDataString(m.Groups[3].Value) 
     ); 
    }