Laravel - 在标题中发送 api_token

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38250569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:06:42  来源:igfitidea点击:

Laravel - Send api_token within the header

apilaravelheaderlumen

提问by user1157885

I'm building an API for Laravel and I want to send the api_token within the header instead of the form post. Is this something that is already built in or would I have to go the route of figuring out how to create my own auth driver?

我正在为 Laravel 构建一个 API,我想在标题中发送 api_token 而不是表单帖子。这是已经内置的东西还是我必须去弄清楚如何创建我自己的身份验证驱动程序?

回答by Andrew

After struggling a little with this myself I got it working. You need to first follow this little tutorial on how to use the api_token for your laravel API: https://gistlog.co/JacobBennett/090369fbab0b31130b51

在我自己为此苦苦挣扎之后,我让它工作了。您需要首先遵循这个关于如何为 Laravel API 使用 api_token 的小教程:https://gistlog.co/JacobBennett/090369fbab0b31130b51

Then once you have the api_token in the users table etc you can now pass this in the header of each request.

然后,一旦您在用户表等中拥有 api_token,您现在就可以在每个请求的标头中传递它。

My laravel is using the Vueify templates, ie I have under /components/Comment.vue etc files.

我的 Laravel 正在使用 Vueify 模板,即我在 /components/Comment.vue 等文件下。

First step is to pass the users api_token to the Vue Template by passing a property through the component definition in your blade template:

第一步是通过刀片模板中的组件定义传递一个属性,将用户 api_token 传递给 Vue 模板:

<comments id_token="{{ access()->user()->api_token }}"></comments>

Then ensure in your .vue file that you accept the property by adding it to the "props":

然后确保在您的 .vue 文件中通过将其添加到“道具”中来接受​​该属性:

export default {
    data: function() {
        return {
            edit: false,
            list: [],
            comment: {
                id: '',
                name: '',
                body: ''
            }
        };
    },

    props: ['id_token'],

    created: function() {
        Vue.http.headers.common['Authorization'] = 'Bearer ' + this.id_token;

        this.fetchCommentList();
    },

Notice above that I also added the token to the common headers in order to have it go through each request used in all the methods further down.

请注意,我还将令牌添加到公共标头中,以便让它通过所有方法中使用的每个请求进一步向下。

Vue.http.headers.common['Authorization'] = 'Bearer ' + this.id_token;

回答by Sangar82

If you are consuming your API, you don't need to create an auth driver, you need to make requests to your API endpoints. Choose the method you prefer, and make requests, don't think as the same way when you use the auth driver at a webpage.

如果您正在使用 API,则不需要创建身份验证驱动程序,您需要向 API 端点发出请求。选择您喜欢的方法,并提出请求,不要像在网页上使用身份验证驱动程序时那样想。

This is an example how send the $token through the headers. With cURL and Guzzle

这是如何通过标头发送 $token 的示例。使用 cURL 和Guzzle

$data = [
    'value1' => 'value1',
    'value2' => 'value2'
];

With CURL

带卷曲

$headers = [
    'Authorization: Bearer '.$token
];

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, 'http://api.domain.com/endpoint');
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch2);
curl_close ($ch2);

With Guzzle

与狂饮

$headers = [
    'Authorization' => 'Bearer '.$token
];

$client = new GuzzleHttp\Client();
$res = $client->request('POST', 'http://api.domain.com/endpoint',[
           'form_params'   => $data,
           'headers'       => $headers,
]);

I hope this helps!

我希望这有帮助!