【发布时间】:2014-09-28 03:06:34
【问题描述】:
目前我正在使用 Facebook API,但遇到了一个小问题(从未使用过 API)。我正在使用最新的 PHP SDK 4.0,人们登录到一个应用程序,该应用程序将他们的访问令牌保存到数据库中。用户可以将多个 Facebook 应用(客户端 ID/秘密)添加到他们的个人资料中,以将执行通知的应用保存在一个位置。
我面临的问题是,我无法将在登录期间检索到的访问令牌与用户添加的应用程序一起使用。所以我的问题是我需要在没有任何用户交互的情况下请求一个新的访问令牌。
也许还有另一种我看不到的方法来使用我在登录过程中检索到的访问令牌和用户添加的客户端 ID/秘密。
当我使用用户添加的任何一对 Client ID/Secret 时,我总是会检索到以下两个错误之一。 (这意味着它们与在登录期间创建的客户端 ID/秘密访问令牌不同)
第一名
Unsupported post request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api
第二名
Invalid appsecret_proof provided in the API argument
这是我需要执行的示例。
// Grab an app from the database
$app = App::find($button->app_id);
// Client ID/Secret
$client_id = $app->app_id;
$client_secret = $app->app_secret;
// Grab the access token for the Client ID/Secret from Facebook
$accessToken = explode('=', file_get_contents(
'https://graph.facebook.com/oauth/access_token?client_id=' . $client_id . '&client_secret=' . $client_secret . '&grant_type=client_credentials'
))[1];
// Init the SDK and set the Client ID/Secret
FacebookSession::setDefaultApplication($client_id, $client_secret);
// Find the user
$user = User::find($request->get('user_id'));
// Create a new session
$session = new FacebookSession($user->access_token);
// If the session has been created
if ($session) {
$canvasPage = 'http://google.com/';
$message = 'This is a test message';
$referrer = 'ref-for-google-canvas';
$response = (new FacebookRequest(
$session, 'POST', '/' . $user->id . '/notifications', array(
'access_token' => $accessToken,
'href' => $canvasPage,
'template' => $message,
'ref' => $referrer
)
))->execute();
}
【问题讨论】:
标签: php facebook facebook-graph-api notifications facebook-access-token