gpt4 book ai didi

php - 在 facebook canvas 应用程序中,一旦我在登录时捕获了签名的请求参数,我应该在哪里发送它

转载 作者:可可西里 更新时间:2023-10-31 22:12:26 24 4
gpt4 key购买 nike

我正在使用 javascript sdk。在文档中它说您从 FB.getLoginStatus() 返回的响应对象中获取签名请求,当用户状态 = 已连接时,但现在我需要解析签名请求。我如何将它发送到我有解析代码的 php 页面?我是否将 php 代码包含在我的 Canvas 应用程序索引页面上,然后将 signedRequest 发送到同一页面上的代码?或者将代码放在单独的页面上并通过 SR。

第一段代码在我的 index.html 页面上。它检查登录状态并从响应对象中获取已签名的请求参数。

第二个 block 是 php 代码,当您通过注册插件捕获它时,facebook 提供解析已签名的请求,但是当您提供其 url 作为参数时,该插件会自动将 SR 发送到此页面。在 Canvas 应用程序中,我必须自己传递它。我怎么做?

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"];

?>

最佳答案

扩展 Clay's Answer你可以用 jQuery.get() 来实现它:

var signed_request = getSignedRequest(), // however you do this..
url = "http://yoursite.com/phppage.php", // wherever your php code is
ajax = $.get(url, {signed_request: signed_request}); // initiate ajax call

// ajax success handler
ajax.done(function(ajaxResponse){
console.log(ajaxResponse);
});

当然,您需要在 PHP 文件中获取该值

$signed_request = $_GET['signed_request'];

关于php - 在 facebook canvas 应用程序中,一旦我在登录时捕获了签名的请求参数,我应该在哪里发送它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17658140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com