0

I am new to APIs, so I don't exactly know how I can use fetch() to get access token and then get user profile. LINE API: [https://developers.line.biz/en/reference/social-api/#issue-access-token] This is all I could figure out, so far. I can get the access token through curl using git bash, but I have no clue on how to write a code in JS to get the json data.

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);
            });
        }
  • What gets logged to the console? – Daniel_Knights Aug 07 '20 at 06:41
  • This @Daniel_Knights - Response {type: "opaque", url: "", redirected: false, status: 0, ok: false, …} body: (...) bodyUsed: false headers: Headers {} ok: false redirected: false status: 0 statusText: "" type: "opaque" url: "" __proto__: Response – Fatma Sheikh Aug 07 '20 at 06:52
  • Is that what you expect? Is the token included? – Daniel_Knights Aug 07 '20 at 06:55
  • @Daniel_Knights No, the token is not included anywhere. I am expecting to get access token, refresh token and all in the console, I guess. And then how can I use it to further fetch data? Like if I get the access token from git bash, can I use it in the code, and how? – Fatma Sheikh Aug 07 '20 at 07:01
  • With `fetch` you have to parse the response: `.then((res) => console.log(res.json()))`. Then you should be able to look through the data in your devtools and see if the token is included. – Daniel_Knights Aug 07 '20 at 07:05
  • @Daniel_Knights It is showing this error on parsing it- Uncaught (in promise) SyntaxError: Unexpected end of input at lineapi.html:33 – Fatma Sheikh Aug 07 '20 at 07:10
  • Don’t use no-cors mode. See the answer at https://stackoverflow.com/a/43268098/441757 – sideshowbarker Aug 07 '20 at 07:10
  • @FatmaSheikh it shouldn't do. Have you typed it correctly? – Daniel_Knights Aug 07 '20 at 07:12
  • @Daniel_Knights I have typed it correctly, I'll try removing the no-cors mode and let you know. – Fatma Sheikh Aug 07 '20 at 07:32
  • @sideshowbarker I referred to your answer so now I'm getting this in the console- Promise {} __proto__: Promise [[PromiseStatus]]: "fulfilled" [[PromiseValue]]: Object error: "invalid_request" error_description: "some parameters missed or invalid" __proto__: Object This is what I changed- fetch( ('https://cors-anywhere.herokuapp.com/'+'https://api.line.me/v2/oauth/accessToken') – Fatma Sheikh Aug 07 '20 at 07:55

0 Answers0