C# Facebook访问令牌-自动获取令牌
到下面看看答案 我可以很好地获得Facebook访问令牌,但我遇到的问题是当我试图自动化这个过程时 如果我访问这个URL是浏览器,我就可以获得访问令牌 例如: 我将其粘贴到浏览器中,然后点击return 然后我被发送到这个页面: 我更改了访问令牌,所以它不是真正的令牌 Viola访问令牌 我试图做的是用代码复制相同的行为。我已经很接近了,但还不太接近 我一直收到用户拒绝错误 %3Ferror%3Daccess\u被拒绝%26错误代码%3D200%26错误描述%3DPermissions%2Error%26错误原因%3Duser\u被拒绝%23\u%3D\u显示=页面和区域设置=en\u US和记录器\u id=786a3153-1d81-415c-8dca-f8fa8b0cd630 我正在将所有标题输出到控制台。这是我关心的位置标题** 我想这和302重定向有关 我在这里收到一个用户拒绝错误。但在浏览器中,我得到了一个非常好的标记。我不明白为什么 位置标题中的标题在使用浏览器时似乎有效 如果无法实现上述功能,则可能出现以下情况: 我想知道有没有带API的浏览器?我可以从命令行调用的东西-传入一些参数-然后获取重定向url是要解析的变量吗 此代码将自动检索访问令牌。您必须具有请求访问令牌的帐户的凭据 更新 首先登录到facebook帐户C# Facebook访问令牌-自动获取令牌,c#,facebook,facebook-graph-api,httpwebrequest,httpwebresponse,C#,Facebook,Facebook Graph Api,Httpwebrequest,Httpwebresponse,到下面看看答案 我可以很好地获得Facebook访问令牌,但我遇到的问题是当我试图自动化这个过程时 如果我访问这个URL是浏览器,我就可以获得访问令牌 例如: 我将其粘贴到浏览器中,然后点击return 然后我被发送到这个页面: 我更改了访问令牌,所以它不是真正的令牌 Viola访问令牌 我试图做的是用代码复制相同的行为。我已经很接近了,但还不太接近 我一直收到用户拒绝错误 %3Ferror%3Daccess\u被拒绝%26错误代码%3D200%26错误描述%3DPermissions%2Err
// LOG INTO FACEBOOK ACCT
string email = "youremail@blah.com";
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();
}
然后请求访问令牌
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)
);
}
你不应该“自动化”这个。在他们的网站上抓取Facebook页面和使用任何自动工具,都明显违反了他们的ToS。@CBroe lmao。我想你不明白这个申请的目的是什么。我拥有授予此应用访问权限的帐户。我只是在一个计划作业上运行这个应用程序,每天获取一个访问令牌,这样我就可以运行我开发的另一个应用程序,每天为许多客户端获取SEO报告,而不是通过前端手动登录每个客户端来下载这些报告。@CBroe问题是访问令牌每60天过期一次。所以现在我已经自动检索访问令牌,所以我不必再碰它了。另外,使用API的一个主要原因是使任务自动化。我不是在刮网站,我只是让应用程序访问我的facebook帐户。“问题是访问令牌每60天过期一次”——这是facebook故意限制用户访问令牌的行为;你不应该回避它。不管你喜欢与否,你所做的就是刮。因此,如果有一天你发现你的应用程序因违反Facebook规则而被Facebook阻止或禁用,不要抱怨;此选项适用于所有用户。3.2:未经我们事先许可,您不得使用自动方式收集用户的内容或信息,或访问Facebook,如获取机器人、机器人、蜘蛛或刮刀。
/// <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)
);
}