2011-11-21 190 views
0

我有一个要求,我只想使用API​​access token使用API​​。
我见过使用Web请求和响应来获取Facebook API的方法,他们也使用重定向URI。Facebook API获取访问令牌

但我真的不能使用重定向URI,因为我想在一个天蓝色的辅助角色中使用它。

喜欢的东西:

facebook a=new facebook(); 
a.appid=""; 

这仅仅是一个可视化的,但任何帮助将是巨大的。

编辑:

下面的答案有帮助。 如果我在浏览器中使用以下URI https://graph.facebook.com/oauth/access_token?client_id=app_Id&client_secret=secret_key&grant_type=client_credentials

它显示的access_token = ### 但如果使用下面的代码做同样在.NET 字符串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(); 

它抛出未经授权

+0

请尽力与适当的标点符号写 - 这使得问题更容易理解。 –

+0

你究竟想知道什么?我想获取访问令牌并以azure工作角色发布到fb粉丝页面。在那里我不能使用正常的请求响应模型来获取访问令牌,因为它需要重定向uri,这对于我的需求来说并不存在。 –

+0

在某些时候,您的用户必须接受您的应用,原因很明显。 – TJHeuvel

回答

1

如果C#API没有提供方法,你可以自己做。访问令牌是以下url的内容。你只需要提出https请求。

https://graph.facebook.com/oauth/access_token?client_id=<APP_ID>&client_secret=<APP_SECRET>&grant_type=client_credentials 

OK,所以这里是工作的PHP代码:

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

这种方法需要创建网址并重定向它,然后登录到Facebook nd然后你得到访问令牌..我不能这样做。 –

+0

它不需要登录Facebook,也不需要重定向。我成功地使用它从PHP使用CURL –

+0

你可以请示例code.it会很好。 –