我该怎么做才能解决这个推送器错误 - 从 auth 端点返回的 JSON 无效,但状态代码为 200? [英] What can I do to resolve this pusher error-JSON returned from auth endpoint was invalid, yet status code was 200?
问题描述
我在这里问了同样的问题后仍然有这个问题:从身份验证端点返回的 JSON 无效,但状态代码为 200 且没有响应.我看过类似的问题并遵循了建议:将我的广播驱动程序设置为pusher",在我的 app.config 文件中取消注释App/BroadcastServiceProvider"类,在我的 .env 文件中将调试模式设置为 false,等等.我还查看了 pusher 文档,但问题仍未解决对我来说.
I still have this problem after asking the same question here: JSON returned from auth endpoint was invalid, yet status code was 200 with no response. I've looked at similar questions and followed the suggestions: setting my broadcast driver to 'pusher', uncommenting 'App/BroadcastServiceProvider' class in my app.config file, setting debug mode to false in my .env file, etc. I have also looked at pusher docs but the issue remains unresolved for me.
我通过添加/broadcasting/auth/"身份验证端点和标头来更新我之前的尝试,但仍然出现相同的错误.但是我现在可以看到 302 重定向到 auth 路由,然后 302 重定向到登录路由,然后到仪表板,在 laravel 望远镜上有 200 响应,我以前没有看到过.所以这向我表明添加 auth 端点应该可以解决问题,但事实并非如此.
I have updated my previous attempt by adding '/broadcasting/auth/' auth endpoint and headers but still the same error. But I can now see a 302 redirect to the auth route then a 302 redirect to the login route then to the dashboard with a 200 response on laravel telescope, which I wasn't seeing before now. So this suggests to me that adding the auth endpoint ought to resolve the issue but it doesn't.
我还尝试设置和使用/pusher/auth/"身份验证端点路由和控制器,但它给了我一个加载资源失败:服务器响应状态为 405(不允许方法)"的提示带有错误:无法从身份验证端点检索身份验证字符串 - 收到状态:来自/pusher/auth 的 405,但不是之前的无效 json 错误.我通过对控制器的获取"请求得到了这个,但通过发布"请求得到了 500 个内部服务器错误.我真的不知道哪个是正确的.
I also tried setting up and using a '/pusher/auth/' auth end point route and controller but it gave me a 'Failed to load resource: the server responded with a status of 405 (Method Not Allowed)' along with "Error: Unable to retrieve auth string from auth endpoint - received status: 405 from /pusher/auth, but not the previous invalid json error. I get this with a 'get' request to the controller but a 500-internal server error with a 'post' request. I really don't know which is correct.
这是我的 bootstrap.js 文件:
This is my bootstrap.js file:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true,
authEndpoint: '/broadcasting/auth',
//authEndpoint: '/pusher/auth',
auth: {
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}',
}
}
});
这是我创建的一个 pusherController:
This is one pusherController I created:
public function pusherAuth(Request $request)
{
$key = getenv('PUSHER_APP_KEY');
$secret = getenv('PUSHER_APP_SECRET');
$app_id = getenv('PUSHER_APP_ID');
$pusher = new Pusher($key, $secret, $app_id);
$auth = $pusher->socket_auth($_GET('channel_name'), $_GET('socket_id'));
return response($auth, 200);
}
我现在知道我的 vue 前端文件应该接收并显示广播签出,问题与解决这个推送器订阅错误有关.
I now know my vue frontend file that should receive and display the broadcast checks out and the issue has to do with resolving this pusher subscription error.
任何帮助将不胜感激.
推荐答案
我终于能够解决这个问题.正如错误消息指出的那样,问题完全是身份验证问题.虽然我仍然不知道为什么内置的/broadcast/auth"端点不起作用,但我最初通过创建/pusher/auth/"来进行身份验证的尝试在我设置路由和控制器的方式上是错误的.
I was finally able to resolve this issue. The problem was entirely an authentication issue as the error messages pointed out. While I still don't know why the built in '/broadcast/auth' endpoint didn't work, my initial attempt to authenticate by creating a '/pusher/auth/' was wrong in the way I set up the route and controller.
正确的路由设置应该是发布"并调用控制器,使用基于闭包的路由对我不起作用.我之前(见上文)控制器的实现也是错误的.
The correct route set up should be 'post' and call a controller, using a closure based route didn't work for me. My previous (see above) implementation of the controller was also wrong.
这是有效的控制器代码:
This is the controller code that worked:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Pusher\Pusher;
class PusherController extends Controller
{
/**
* Authenticates logged-in user in the Pusher JS app
* For private channels
*/
public function pusherAuth(Request $request)
{
$user = auth()->user();
$socket_id = $request['socket_id'];
$channel_name =$request['channel_name'];
$key = getenv('PUSHER_APP_KEY');
$secret = getenv('PUSHER_APP_SECRET');
$app_id = getenv('PUSHER_APP_ID');
if ($user) {
$pusher = new Pusher($key, $secret, $app_id);
$auth = $pusher->socket_Auth($channel_name, $socket_id);
return response($auth, 200);
} else {
header('', true, 403);
echo "Forbidden";
return;
}
}
}
这是最终的 bootstrap.js 文件:
This is the final bootstrap.js file:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true,
authEndpoint: '/pusher/auth',
auth: {
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}',
}
}
});
还有我在 web.php 中的路由代码:
And my route code in web.php:
Route::post('/pusher/auth', [PusherController::class, 'pusherAuth'])
->middleware('auth');
Pusher 控制台日志:推送器::[事件记录",{事件":pusher_internal:subscription_succeeded",频道":private-user.3",数据":{}}]vendor.js:41325 Pusher : : [pusher:subscription_succeeded 在 private-user.3 上没有回调"]
Pusher console log: Pusher : : ["Event recd",{"event":"pusher_internal:subscription_succeeded","channel":"private-user.3","data":{}}] vendor.js:41325 Pusher : : ["No callbacks on private-user.3 for pusher:subscription_succeeded"]
这篇关于我该怎么做才能解决这个推送器错误 - 从 auth 端点返回的 JSON 无效,但状态代码为 200?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!