JavaScript SDK を使用しています。ドキュメントでは、ユーザーのステータスが接続されているときに FB.getLoginStatus() が返す応答オブジェクトから署名付きリクエストを取得すると書かれていますが、署名付きリクエストを解析する必要があります。解析コードがあるphpページに送信するにはどうすればよいですか? キャンバス アプリのインデックス ページに php コードを含めてから、signedRequest を同じページのコードに送信しますか? または、コードを別のページに保管して、SR を渡します。
コードの最初のブロックは、私の index.html ページにあります。ログイン ステータスを確認し、応答オブジェクトから署名済みのリクエスト パラメータを取得します。
2 番目のブロックは、登録プラグインを介して取得した署名付きリクエストを解析するために facebook が提供する php コードですが、URL をパラメータとして指定すると、プラグインはこのページに SR を自動的に送信します。キャンバス アプリでは、自分で渡す必要があります。それ、どうやったら出来るの?
JavaScript
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
var signedRequest = response.authResponse.signedRequest;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
} else {
// the user isn't logged in to Facebook.
}
});
PHP ページ
<?php
define('FACEBOOK_APP_ID', '3*****88&'); // Place your App Id here
define('FACEBOOK_SECRET', '1345*****eb4f2da'); // Place your App Secret Here
// No need to change the function body
function parse_signed_request($signed_request, $secret)
{
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256')
{
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig)
{
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}
if ($_REQUEST)
{
$response = parse_signed_request($_REQUEST['signed_request'], FACEBOOK_SECRET);
}
$name = $response["registration"]["name"];
$email = $response["registration"]["email"];
$password = $response["registration"]["password"];
$uID = $response["user_id"];
?>