如何使用 curl 生成 Facebook 用户访问令牌
How to generate Facebook User Access Token using curl
我正在做一个需要导入 Facebook 页面提要的项目。要访问 Facebook 页面提要,我需要 page_access_token 并生成 page_access_token 我需要用户访问令牌。
我的问题是
1.How 使用 CURL 生成此 User_access_token?大多数解决方案需要 APP_KEY & APP_SECRET。没有任何APP是不是不能获取user_access_token?
- 获得 User_access_token 后,我如何使用它通过 CURL 获取页面访问令牌。
没有应用程序你无法获得任何令牌,但你不需要编写任何程序来获得用户令牌。这些文章详细解释了一切:
- https://developers.facebook.com/docs/facebook-login/access-tokens
- http://www.devils-heaven.com/facebook-access-tokens/
- https://developers.facebook.com/docs/facebook-login/
例如,您可以使用 API Explorer 来 select 您的应用程序并生成用户令牌。
错误的问题,不需要令牌
我刚试了一下,用了不到 5 分钟,以前从来没有刷过 FB。我将该页面保存到我的服务器并使用我的 URL 调出该页面,它看起来就像我在 FB 上一样。
如果浏览器可以在禁用 JavaScript 的情况下加载页面,那么您也可以。
您必须使用 https://m.facebook.com/,JavaScript 在他们的移动网站上不是必需的。
你想做的,一点都不难
只需在您的浏览器中转到那里并将 cookie 键值复制到 Cookie:
HTTP 请求 header 中。我的已经x了。
<?php
$request = array();
$request[] = 'Host: m.facebook.com';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request[] = 'Accept-Language: en-US,en;q=0.5';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'DNT: 1';
$request[] = 'Cookie: datr=x; fr=x; lu=x s=xx; csm=x; xs=xx; c_user=x; p=-2; act=x; presence=x; noscript=1';
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'ache-Control: no-cache';
$url = 'https://m.facebook.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,100);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader = substr($data,0,$skip);
$data= substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
while(true){ // get cookies from response header
$s = strpos($head,'Set-Cookie: ',$e);
if (!$s){break;}
$s += 12;
$e = strpos($head,';',$s);
$cookie = substr($head,$s,$e-$s) ;
$s = strpos($cookie,'=');
$key = substr($cookie,0,$s);
$value = substr($cookie,$s);
$cookies[$key] = $value;
}
$cookie = ''; // format cookies for next request header
$show = '';
$head = '';
$delim = '';
foreach ($cookies as $k => $v){
$cookie .= "$delim$k$v";
$delim = '; ';
}
$fp = fopen("fb.html",'w');
fwrite($fp,"$data\n$info\n$responseHeader");
fclose($fp);
readfile('fb.html');
我正在做一个需要导入 Facebook 页面提要的项目。要访问 Facebook 页面提要,我需要 page_access_token 并生成 page_access_token 我需要用户访问令牌。
我的问题是
1.How 使用 CURL 生成此 User_access_token?大多数解决方案需要 APP_KEY & APP_SECRET。没有任何APP是不是不能获取user_access_token?
- 获得 User_access_token 后,我如何使用它通过 CURL 获取页面访问令牌。
没有应用程序你无法获得任何令牌,但你不需要编写任何程序来获得用户令牌。这些文章详细解释了一切:
- https://developers.facebook.com/docs/facebook-login/access-tokens
- http://www.devils-heaven.com/facebook-access-tokens/
- https://developers.facebook.com/docs/facebook-login/
例如,您可以使用 API Explorer 来 select 您的应用程序并生成用户令牌。
错误的问题,不需要令牌
我刚试了一下,用了不到 5 分钟,以前从来没有刷过 FB。我将该页面保存到我的服务器并使用我的 URL 调出该页面,它看起来就像我在 FB 上一样。
如果浏览器可以在禁用 JavaScript 的情况下加载页面,那么您也可以。
您必须使用 https://m.facebook.com/,JavaScript 在他们的移动网站上不是必需的。
你想做的,一点都不难
只需在您的浏览器中转到那里并将 cookie 键值复制到 Cookie:
HTTP 请求 header 中。我的已经x了。
<?php
$request = array();
$request[] = 'Host: m.facebook.com';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request[] = 'Accept-Language: en-US,en;q=0.5';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'DNT: 1';
$request[] = 'Cookie: datr=x; fr=x; lu=x s=xx; csm=x; xs=xx; c_user=x; p=-2; act=x; presence=x; noscript=1';
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'ache-Control: no-cache';
$url = 'https://m.facebook.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,100);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader = substr($data,0,$skip);
$data= substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
while(true){ // get cookies from response header
$s = strpos($head,'Set-Cookie: ',$e);
if (!$s){break;}
$s += 12;
$e = strpos($head,';',$s);
$cookie = substr($head,$s,$e-$s) ;
$s = strpos($cookie,'=');
$key = substr($cookie,0,$s);
$value = substr($cookie,$s);
$cookies[$key] = $value;
}
$cookie = ''; // format cookies for next request header
$show = '';
$head = '';
$delim = '';
foreach ($cookies as $k => $v){
$cookie .= "$delim$k$v";
$delim = '; ';
}
$fp = fopen("fb.html",'w');
fwrite($fp,"$data\n$info\n$responseHeader");
fclose($fp);
readfile('fb.html');