2

当驱动程序是 api 时,下面的代码运行良好。然后我创建了一个新项目并将驱动程序更改为护照。

现在,我总是收到错误:未经授权。我可以确认请求标头在浏览器中具有授权令牌代码。请通过单击查看下面的图像,然后单击缩放以查看质量更好的图像。

在此处输入图像描述

我在下面的代码中遗漏了什么吗?如果您需要更多信息,请告诉我。

引导程序.js

import Echo from 'laravel-echo'
window.Echo = new Echo({
   broadcaster: 'socket.io',
   host: window.location.hostname + ':6001',
   auth: {
       headers: {
           Authorization: 'Bearer' + localStorage.getItem("token")
       }
   }
});

使用 Web socket.io 发布消息的代码

var config = { 
    headers : {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + localStorage.getItem('token')
    }
};

var request = {
    "channel": "private-Send-Message-Channel." + message.Sent_To,
    "name": "MessengerEvent",
    "data": {
        "msg": message
    },
    "socket_id": socketId
};
var url = "http://localhost:6001/apps/786d1c1c820025f4/events?auth_key=be1699f0101880f673fdff7ff203334f";

axios.post(url, JSON.stringify(request), this.config).then(response => {
    //success callback
});

当最后一行代码运行 ( axios.post) 时,它给出 403, Unauthorized exception

我可以确认令牌存在于标头中。

更新 - 1

我有两个示例项目。一个带有驱动程序 = api,另一个带有驱动程序 = 护照。当我使用 driver : api 时,没有问题。上述问题仅适用于司机 = 护照。

4

1 回答 1

-1

您是否遵循了护照文件:https ://laravel.com/docs/5.6/passport ?

为了获得 OAuth 身份验证,除了不记名令牌之外,您还需要在正文中添加更多参数。

 {
        "grant_type":"password",
        "client_id":"CLIENT_ID",
        "client_secret":"YOUR_SECRET",
        "username":"USERNAME",
        "password":"PASSWORD",
        "scope":""
    }
于 2018-03-09T10:54:26.287 回答