Forbidden error When Accessing WaniKani API

Hello

I’m currently facing an issue with my WaniKani API integration in my custom application. I’m trying to fetch user progress data using the API, but I keep receiving error code 403 (Forbidden) despite providing the correct API key.

Here’s a snippet of the code I’m using:

import requests

api_key = ‘your_api_key_here’
url = ‘https://api.wanikani.com/v2/user
headers = {‘Authorization’: ‘Bearer {}’.format(api_key)}

response = requests.get(url, headers=headers)

print(response.status_code)
print(response.json())

I’ve checked my API key, and it seems to be valid. Could anyone provide some help ; why I might be receiving this error and how to resolve it? WaniKani - WaniKani Community info

Thank you in advance for your help!

Thank you
gregbowers

1 Like

I don’t see any immediate problems with your code. Maybe double-check you don’t have any spaces or extra characters in the token value.

I’m using similar code myself. Just checked that it works.

from dotenv import dotenv_values

import requests
import requests.auth


class BearerTokenAuth(requests.auth.AuthBase):
    def __init__(self, token) -> None:
        super().__init__()
        self._token = token

    def __call__(self, r: requests.PreparedRequest) -> requests.PreparedRequest:
        r.headers["Authorization"] = f"Bearer {self._token}"
        return r


class WaniKaniApi:
    base_url = "https://api.wanikani.com/v2"

    def __init__(self, token: str) -> None:
        self._auth = BearerTokenAuth(token)

    def get_user(self) -> dict:
        r = requests.get(
            url=f"{self.base_url}/user",
            auth=self._auth,
        )
        if r.ok:
            return r.json()
        else:
            print(r.text)
            r.raise_for_status()


if __name__ == "__main__":
    env = dotenv_values()
    wkapi = WaniKaniApi(env["WK_TOKEN"])

    r = wkapi.get_user()
    print(r)

this assumes you have a .env file like this:

WK_TOKEN=123-abc-456-def

and of course you’d need to install python-dotenv package.

Actually, checked your snippet. It works on my end. Also tried a wrong value for the token and the expected error is 401 Anauthorized.

So I think your token is correct but for some reason you don’t have access to the user endpoint :person_shrugging:

Might be because you don’t have an active subscription? But that’s strange for a generic endpoint such as /user.

Try creating a new token maybe.

2 Likes

Done some of my own testing on a completely fresh account with no active subscription: even a token with none of the optional permissions still gets a 200 with OP’s code snippet. If a new token doesn’t fix things, maybe it’s some kind of IP blacklisting thing?