【问题标题】:Facebook PHP SDK - One Access Token, Multiple AppsFacebook PHP SDK - 一个访问令牌,多个应用程序
【发布时间】: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


    【解决方案1】:

    用户令牌的有效期仅为 2 小时。您可以将它们延长 60 天,但没有永久有效的用户令牌。如果用户长时间不使用应用程序,应用程序将无法发布给用户。话虽如此,您不会将用户令牌用于应用通知,请查看文档:https://developers.facebook.com/docs/games/notifications

    您只需要一个应用令牌,并且该令牌永远有效:

    $app_token = $appId . '|' . $appSecret;
    

    关于 appsecret_proof 错误:您可能在应用设置中激活了它。要么停用它,要么将 appsecret_proof 添加到服务器上的每个 API 调用中。在 Facebook 文档中了解更多信息:https://developers.facebook.com/docs/graph-api/securing-requests

    顺便说一句,在没有 PHP SDK 的情况下发送应用通知非常容易,请查看这篇文章:http://blog.limesoda.com/2014/08/app-notifications-facebook-apps/ - 它是德语,但无论如何,与您唯一相关的部分是代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 2013-01-03
      • 2013-04-11
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多