# Uncaught SyntaxError: Unexpected token p in JSON at position 4?

**URL:** <https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746>\
**Category:** API\
**Created:** [May 19, 2020, 12:18pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746 "2020-05-19T12:18:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoxusDestinY](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@NoxusDestinY](https://discuss.dev.twitch.com/u/NoxusDestinY)\
**Post date:** [May 19, 2020, 12:18pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/1 "2020-05-19T12:18:30Z")

</div>

Hello everyone, i’m a french novice developer and,i’ve a problem with my background.js and app.js.

I want do an API twitch request with XMLHttpRequest but Google Chrome console say that:

 ![Exemple](https://d2opxh93rbxzdn.cloudfront.net/original/2X/5/51d4abd4f2a8c291b7e3ab0178232b23ebcc7776.png)

I will send my .js codes lines after 🙂

THX

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 19, 2020, 12:20pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/2 "2020-05-19T12:20:11Z")

</div>

Looks like you are calling `id.twitch.tv/streams`

Thats not a valid endpoint, you are getting a 404 http error and not processing that before attempting to JSON decode

Streams API is documented here:

> **[Reference](https://dev.twitch.tv/docs/api/reference/)**
>
> Twitch Developer tools and services to integrate Twitch into your development or create interactive experience on twitch.tv.

---

<div class="post-metadata">

**Author:** ![NoxusDestinY](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@NoxusDestinY](https://discuss.dev.twitch.com/u/NoxusDestinY)\
**Post date:** [May 19, 2020, 12:28pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/3 "2020-05-19T12:28:38Z")

</div>

Okay thx but i’v another problem now 🙃

 ![Exemple](https://d2opxh93rbxzdn.cloudfront.net/original/2X/c/ce43ff504e8b31bab04f972462863781a53e257f.png)

Sry dude i’m bad in JavaScript programming language 😅

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 19, 2020, 12:30pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/4 "2020-05-19T12:30:40Z")

</div>

This tells me nothing.

What are you _actually_ trying to do, and what code do you have thats trying to do it

---

<div class="post-metadata">

**Author:** ![NoxusDestinY](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@NoxusDestinY](https://discuss.dev.twitch.com/u/NoxusDestinY)\
**Post date:** [May 19, 2020, 12:34pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/5 "2020-05-19T12:34:47Z")

</div>

I trying to know when streamer is online or offline to notify users

 ![Exemple](https://d2opxh93rbxzdn.cloudfront.net/original/2X/8/8ddee0d412fcdc33c5f471e911b75cc21611f44c.png)

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 19, 2020, 12:38pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/6 "2020-05-19T12:38:54Z")

</div>

1. Client-ID needs to be a header
2. You are missing an oAuth token - [Requiring OAuth for Helix Twitch API Endpoints](https://discuss.dev.twitch.com/t/requiring-oauth-for-helix-twitch-api-endpoints/23916/)
3. Your URL is constructed wrong

Should be

> [http://api.twitch.tv/helix/streams?user\_login=anaee](http://api.twitch.tv/helix/streams?user_login=anaee)

1. these days it’s easier for your front end to call your backend and the backend makes the call to Twitch with an App Access Token - [Getting OAuth Access Tokens | Twitch Developers](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-client-credentials-flow)

---

<div class="post-metadata">

**Author:** ![NoxusDestinY](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@NoxusDestinY](https://discuss.dev.twitch.com/u/NoxusDestinY)\
**Post date:** [May 19, 2020, 1:03pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/7 "2020-05-19T13:03:50Z")

</div>

To get the curl -H ‘Authorization:\*\*\*\*\*’  
we need to do :

```auto
POST https://id.twitch.tv/oauth2/token
    ?client_id=<your client ID>
    &client_secret=<your client secret>
    &grant_type=client_credentials
    &scope=<space-separated list of scopes>

```

but where i put it (sry dude)

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 19, 2020, 1:05pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/8 "2020-05-19T13:05:27Z")

</div>

This would be done on a backend system which would collect the token and store it for reuse, most commonly a cron job, or other service for handling fetch/store/maintatin of a token

---

<div class="post-metadata">

**Author:** ![NoxusDestinY](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@NoxusDestinY](https://discuss.dev.twitch.com/u/NoxusDestinY)\
**Post date:** [May 19, 2020, 1:16pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/9 "2020-05-19T13:16:47Z")

</div>

I’m losted, it’s complicated  
how can I do this 😂

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 19, 2020, 1:25pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/10 "2020-05-19T13:25:17Z")

</div>

You need an oAuth token to make requests to helix

If you are not logging the user into your website via oAuth that means an App Access Token

Whilst you can, you should generate an App Access Token each time you need to call Helix you should create one and store and use it till it expires.

You can’t use the oAuth Token on the front end as it constitutes a password and needs to be protected.

So your front end needs to call the server, the server recalls the preexisting App Access Token and calls helix to get the live stream status.

That said, since it’s also a server you can use webhooks instead which means you don’t have to call helix at all, Twitch will tell you when a stream starts and ends. Then you just store the Stream status in your database

[https://dev.twitch.tv/docs/api/webhooks-guide](https://dev.twitch.tv/docs/api/webhooks-guide)

---

<div class="post-metadata">

**Author:** ![NoxusDestinY](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@NoxusDestinY](https://discuss.dev.twitch.com/u/NoxusDestinY)\
**Post date:** [May 19, 2020, 1:49pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/11 "2020-05-19T13:49:38Z")

</div>

Okay thx i go to do that

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [June 18, 2020, 1:49pm UTC](https://discuss.dev.twitch.com/t/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-4/25746/12 "2020-06-18T13:49:39Z")

</div>

Closed on June 18, 2020, 1:49pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
