Facebook API to get access token
I have a requirement where I want to use Facebookaccess token using only APIs.
I have seen methods which works using web requests and responses to get the Facebook API and they also use redirect URIs.
But I cant really use redirect URI since I want to use it in a azure worker role.
Something like:
facebook a=new facebook();
a.appid="";
This is just a visualization but any help would be great.
EDIT:
Below answer helps. and if i use following uri in browser https://graph.facebook.com/oauth/access_token?client_id=app_Id&client_secret=secret_key&grant_type=client_credentials
it shows access_token=### but if use following code to do the same in .net string url = "https://graph.facebook.com/oauth/access_token?client_id=app_id &client_secret=secretKey &grant_type=client_credentials";
string token;
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
it throws unauthorized
If the C# API doesn't provide method for it, you can do it yourself. The access token is the content of following url. You just have to make https request.
https://graph.facebook.com/oauth/access_token?client_id=<APP_ID>&client_secret=<APP_SECRET>&grant_type=client_credentials
OK, so here is working PHP code:
function getAccessToken(){
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&grant_type=client_credentials';
return makeRequest($token_url);
}
function makeRequest( $url, $timeout = 5 ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "PHP_APP" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
if($response['http_code'] == 200) return $content;
return FALSE;
}
链接地址: http://www.djcxy.com/p/61604.html
下一篇: Facebook API获取访问令牌