Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[graphql-python] 'str' object has no attribute 'decode' #1242

Closed
circlensq opened this issue Dec 29, 2020 · 3 comments
Closed

[graphql-python] 'str' object has no attribute 'decode' #1242

circlensq opened this issue Dec 29, 2020 · 3 comments

Comments

@circlensq
Copy link

circlensq commented Dec 29, 2020

I follow this tutorial graphql-python/4-authentication
Dependencies:

Django==2.2.17
django-filter==2.0.0
django-graphql-jwt==0.2.1
graphene==2.1.8
graphene-django==2.2.0
graphql-core==2.3.2
graphql-relay==2.0.1
PyJWT==1.7.1 (I found error: module 'jwt' has no attribute 'ExpiredSignature' when use PyJWT==2.0.0, and fix it after downgraded it)

and getting this error while doing
GraphQL Query:

mutation {
  tokenAuth(username: "joanatas", password: "123456A!"){
    token
  }
}

Error:

{
  "errors": [
    {
      "message": "'str' object has no attribute 'decode'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "tokenAuth"
      ]
    }
  ],
  "data": {
    "tokenAuth": null
  }
}

I fix this problem by removing .decode('utf-8') in the graphql_jwt/utils.py.
From:

def jwt_encode(payload, context=None):
    return jwt.encode(
        payload,
        jwt_settings.JWT_SECRET_KEY,
        jwt_settings.JWT_ALGORITHM,
    ).decode('utf-8')

to be:

def jwt_encode(payload, context=None):
    return jwt.encode(
        payload,
        jwt_settings.JWT_SECRET_KEY,
        jwt_settings.JWT_ALGORITHM,
    )

My question is, is there any drawbacks or side effects after I do this or is there any better way? Thank you.

@VerzCar
Copy link

VerzCar commented Jan 9, 2021

Hi, have the same issue here.

My Dependencies:
Django = "^3.1.3"
django-cors-headers = "^3.5.0"
django-graphql-jwt = "^0.3.1"
django-split-settings = "^1.0"
django-axes = "^5.9"
django-csp = "^3.7"
django-health-check = "^3.16"
django-http-referrer-policy = "^1.1"
django-feature-policy = "^3.5"
django-stubs-ext = "^0.1"

graphene = "^2.1.8"
graphene-django = "^2.13.0"
graphql-core = "^2.3.2"
graphql-relay = "^2.0.1"

Any update?

@circlensq
Copy link
Author

I still use the same approach to fix this problem. Try to remove .decode('utf-8') in the graphql_jwt/utils.py.
From:

def jwt_encode(payload, context=None):
    return jwt.encode(
        payload,
        jwt_settings.JWT_SECRET_KEY,
        jwt_settings.JWT_ALGORITHM,
    ).decode('utf-8')

to be:

def jwt_encode(payload, context=None):
    return jwt.encode(
        payload,
        jwt_settings.JWT_SECRET_KEY,
        jwt_settings.JWT_ALGORITHM,
    )

the error comes when your object actually have already been encoded, so you don't have to do it twice.
reference: https://stackoverflow.com/questions/28583565/str-object-has-no-attribute-decode-python-3-error

@clebio
Copy link

clebio commented Mar 23, 2021

This comment: flavors/django-graphql-jwt#241 (comment)

suggests that you can use PyJWT version 1.7.0:

pip install PyJWT==1.7.0

Which would suggest that the getting started guide should be updated to include that:

pip install PyJWT==1.7.0 django==2.1.4 graphene-django==2.2.0 django-filter==2.0.0 django-graphql-jwt==0.1.5

https://www.howtographql.com/graphql-python/1-getting-started/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants