# Confused about signing JWT

**URL:** <https://discuss.dev.twitch.com/t/confused-about-signing-jwt/11928>\
**Category:** Extensions\
**Created:** [September 11, 2017, 7:41pm UTC](https://discuss.dev.twitch.com/t/confused-about-signing-jwt/11928 "2017-09-11T19:41:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nilxoc](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/nilxoc/32/3837_2.png) [@Nilxoc](https://discuss.dev.twitch.com/u/Nilxoc)\
**Post date:** [September 11, 2017, 7:41pm UTC](https://discuss.dev.twitch.com/t/confused-about-signing-jwt/11928/1 "2017-09-11T19:41:37Z")

</div>

I’m currently trying to understand the Pubsub API, specifically how I can send messages to it from my EBS. However after a long time of trying I still can’t send any request that doesn’t result in an authenthication failure. For testing purposes I am just trying to request my extension secret from the command line using curl. I hope somebody can help me figure out why this won’t generate valid JWTs.

Here is the Python script I use to generate the JWT:

```
import jwt;
import time;
import math;
import base64;

payload = {
        'exp': math.floor(time.time())+(60*60),
        'user_id': 61960862,
        'role' : 'external'
};

secret = base64.decodebytes(b'my_secret');
result = jwt.encode(payload,secret, algorithm = 'HS256');
print(result);

```

my\_secret is the secret I get from the secret keys section of my extension’s settings.  
jwt is a library called pyjwt

I then try to send my request using the following command:

```
    curl -H 'Authorization: Bearer '`python generateJWT.py` \
        -H 'Client-Id: extension_client_id' \
        -X GET https://api.twitch.tv/extensions/extension_client_id/auth/secret

```

extension\_client\_id represents the client ID from the overview page of my extension

As a response I get {“error”:“Unauthorized”,“status”:401,“message”:"{“code”:401,“status”:“authentication failed”}\n"}

I’m assuming that I’m getting this response because I’m somehow signing my JWT wrong but I’m at wit’s end as to why that is.

---

<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:** [September 11, 2017, 7:44pm UTC](https://discuss.dev.twitch.com/t/confused-about-signing-jwt/11928/2 "2017-09-11T19:44:47Z")

</div>

> [@Nilxoc](#):
>
> payload = {  
> ‘exp’: math.floor(time.time())+(60\*60),  
> ‘user\_id’: 61960862,  
> ‘role’ : ‘external’  
> };

You need to cast user\_id to a string

```
payload = {
    'exp': math.floor(time.time())+(60*60),
    'user_id': str(61960862),
    'role' : 'external'
};

```

Should work for python

---

<div class="post-metadata">

**Author:** ![Nilxoc](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/nilxoc/32/3837_2.png) [@Nilxoc](https://discuss.dev.twitch.com/u/Nilxoc)\
**Post date:** [September 11, 2017, 8:11pm UTC](https://discuss.dev.twitch.com/t/confused-about-signing-jwt/11928/3 "2017-09-11T20:11:42Z")

</div>

Thank you for your help, you were certainly right. There was also an issue with my script where it printed the JWT in byte format (b’token’). For future reference I solved this by decoding the result in utf-8.  
(result = result.decode(“utf-8”)). Just in case anyone stumbles upon this with a similar problem.
