首页 > 解决方案 > 如何在 JavaScript 中从 LINE api 获取用户数据?

问题描述

我是 API 新手,所以我不完全知道如何使用 fetch() 获取访问令牌,然后获取用户配置文件。LINE API:[https://developers.line.biz/en/reference/social-api/#issue-access-token] 到目前为止,这就是我能想到的。我可以使用 git bash 通过 curl 获取访问令牌,但我不知道如何在 JS 中编写代码来获取 json 数据。

function generate(e){
         e.preventDefault();
          fetch(('https://cors-anywhere.herokuapp.com/'+'https://api.line.me/v2/oauth/accessToken'), {
               method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                    },
                body: JSON.stringify({
                    grant_type: 'authorization_code',
                    client_id: 'my_id',
                    client_secret: 'my_client_secret',
                    code: 'code',
                    redirect_uri: 'the link direct to'
                })
            })
            .then((res) => console.log(res.json()))
            .catch((err) => {
                console.log(err);
            });
        }

标签: javascriptfetchfetch-api

解决方案


推荐阅读