与Facebook PHP SDK进行匿名通话 [英] Make anonymous call with Facebook PHP SDK
问题描述
在我的PHP应用程序中,用户提供自己的Facebook应用程序ID和应用程序秘密。我需要验证它们,并显示错误,如果它们无效。
我已经找到了一个很好的方式来做到这一点。我可以请求 https://graph.facebook .com / oauth / access_token?client_id = 123456& client_secret = abcdefg& grant_type = client_credentials
如果凭据无效,则响应如下: p>
{
error:{
message:验证应用程序时出错,无法获取应用信息系统错误,
type:OAuthException,
code:101,
fbtrace_id:D8oHjJoc2Nc
}
}
我对使用PHP SDK的方式感到困惑。有一个整齐的 get()
方法来提出这样的请求,但我不知道如何发送请求而不授权应用程序。这是我做的:
$ app = new Facebook\FacebookApp($ app_id,$ app_secret);
$ access_token = $ app-> getAccessToken();
$ query = http_build_query([
'client_id'=> $ app_id,
'client_secret'=> $ app_secret,
'grant_type'=> 'client_credentials',
]);
$ facebook = new Facebook\Facebook([
'app_id'=> $ app_id,
'app_secret'=> $ app_secret,
'default_graph_version '=>'2.5',
]);
$ response = $ facebook-> get('/ oauth / access_token?'$ query,$ access_token);
我收到以下错误:
< blockquote>
未知的路径组件:/ oauth / access_token
但即使它有效,也很奇怪,它与任何发件人凭据。是否可以使用PHP SDK做出匿名的Facebook请求?
SDK会将指定的API版本号添加到在 - > get()中的路径,所以我认为这是导致你的错误在这里,因为底层的调用正在进行 /2.5/oauth/access_token
(失败了我在一个浏览器)
应该是 /v2.5/oauth/access_token
(在浏览器中为我工作) / p>
将 default_graph_version
更新为 v2.5
并尝试
In my PHP application, users provide their own Facebook Application ID and Application Secret. I need to validate them and display nice error if they are invalid.
I already found a nice way to do it. I can make a request to https://graph.facebook.com/oauth/access_token?client_id=123456&client_secret=abcdefg&grant_type=client_credentials
If credentials are invalid, the response is as follows:
{
"error": {
"message": "Error validating application. Cannot get application info due to a system error.",
"type": "OAuthException",
"code": 101,
"fbtrace_id": "D8oHjJoc2Nc"
}
}
I'm confused about the ways to do it with PHP SDK. There's a neat get()
method to make such a request, but I'm not sure how to send request without authorizing the application. This is what I did:
$app = new Facebook\FacebookApp( $app_id, $app_secret );
$access_token = $app->getAccessToken();
$query = http_build_query([
'client_id' => $app_id,
'client_secret' => $app_secret,
'grant_type' => 'client_credentials',
]);
$facebook = new Facebook\Facebook( [
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => '2.5',
] );
$response = $facebook->get( '/oauth/access_token?' . $query, $access_token );
I'm getting the following error:
Unknown path components: /oauth/access_token
But even if it worked, it's strange to call it with any sender credentials. Is it possible to make an "anonymous" Facebook request with PHP SDK?
The SDK implicitly adds the API version number specified to the path in -> get(), so I think that's causing your error here because the underlying call is being made to /2.5/oauth/access_token
(fails for me in a browser)
It should be /v2.5/oauth/access_token
(works for me in a browser)
Update default_graph_version
to v2.5
and try that
这篇关于与Facebook PHP SDK进行匿名通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!