1

I am using the fetch api to get an access token returned from the github api.

When I check the network tab I see that the token is returned but I am unable to access it in my fetch request.

My code looks like this:

fetch(`https://github.com/login/oauth/access_token?client_id=***&client_secret=***&code=${code}&redirect_uri=http://localhost:3000/&state=react`, {
    method: 'POST',
    mode: 'no-cors',
    headers: new Headers({
      'Content-Type': 'application/json'
    })
  }).then(function(res) {
    console.log(res); // I have already tried return res.json() here
  })

The console displays the following error if I return res.json():

index.js:30 Uncaught (in promise) SyntaxError: Unexpected end of input

The GitHub docs states the response takes the following format:

By default, the response takes the following form:

access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&token_type=bearer

I guess it isn't returning valid json but just a string so I am not sure how to access this response.

The response looks like this:

enter image description here

However, when I try and log out the response I get SyntaxError: Unexpected end of input

6
  • 2
    Can you check if res is a valid JSON? Commented Apr 16, 2018 at 11:02
  • Just going to leave this here to save you a lot of time : developer.chrome.com/devtools Commented Apr 16, 2018 at 11:03
  • I have been using the console. If you read the question I have shown the error in the console Commented Apr 16, 2018 at 11:04
  • Also you have 2x then, but I only see 1 promise.. Yes you are seeing error in console, but you are not debugging.. Breakpoint ? Commented Apr 16, 2018 at 11:04
  • @31piy I don't think it is valid son but I am not sure how to get the response - updated question with ref to GitHub docs Commented Apr 16, 2018 at 11:05

2 Answers 2

1

If you are using mode: 'no-cors, browser will restrict to access body. Browser has security for cross domain. If you want to access body you have to call without mode: 'no-cors property.

https://developer.mozilla.org/en-US/docs/Web/API/Request/mode

This will work

fetch(`https://jsonplaceholder.typicode.com/posts/1`, {
  method: 'GET',
  headers: new Headers({
    'Content-Type': 'application/json'
  })
})
.then(res => res.json())
.then(function(res) {
  console.log(res);
})

This will not work

fetch(`https://jsonplaceholder.typicode.com/posts/1`, {
  method: 'GET',
  mode: 'no-cors',
  headers: new Headers({
    'Content-Type': 'application/json'
  })
})
.then(res => res.json())
.then(function(res) {
  console.log(res);
})
Sign up to request clarification or add additional context in comments.

4 Comments

I need to use mode: no-cors to allow the request
Is there any specific reason to use no-cors. If github restricting you by cross domain, you have to remove no-cors and create your own backend api which get data from github.
I am getting the response returned, but I am unable to access it in the fetch request
Yes, browser will take response but will not give acces to JavaScript as security policy.
0

I think you're almost there. You've mentioned this link to the docs. If you read further, you can see that to get response in JSON, you need to include a header named Accept with the value of application/json.

fetch(` ... `, {
    method: 'POST',
    mode: 'no-cors',
    headers: new Headers({
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    })
  }).then(function(res) {
    ...
  })

This way, you can apply .json() on res.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.