繁体   English   中英

Facebook API获取访问令牌

[英]Facebook API to get access token

我有一个要求,我想仅使用API​​使用Facebookaccess令牌。
我已经看到了使用Web请求和响应来获取Facebook API的方法,并且它们还使用了重定向URI。

但是我真的不能使用重定向URI,因为我想在Azure角色中使用它。

就像是:

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

这只是一个可视化,但任何帮助都将非常有用。

编辑:

下面的答案有帮助。 如果我在浏览器https://graph.facebook.com/oauth/access_token中使用以下uri,则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();

它抛出未经授权

如果C#API不提供此方法,则可以自己执行。 访问令牌是以下URL的内容。 您只需要发出https请求即可。

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

好的,这是可以使用的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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM