0
votes

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

1
Please make an effort to write with proper punctuation - that makes a question easier to understand.Roman Starkov
what exactly you would like to know? i want to get access token and post to fb fan page in azure worker role. where i cant use normal request response model to get access token as it requires redirect uri which does not exist for my requirement.Mandar Jogalekar
At some point your user has to accept your app, for obvious reasons.TJHeuvel
i want to post as a part of batch schedulling so only admin can post to the wall at specific intervals.Mandar Jogalekar

1 Answers

1
votes

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