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

Cannot get API to authenticate #352

Open
timkicker opened this issue Apr 28, 2024 · 3 comments
Open

Cannot get API to authenticate #352

timkicker opened this issue Apr 28, 2024 · 3 comments

Comments

@timkicker
Copy link
Contributor

Hi! I am currently working on a manual scrobbler for maloja in python, however i am not able to authenticate with my server. I tried it like many other api's through the exposed challenge (seen in my code below) and by just using the apikey as my token with no success.

How do i use the api the correct way?

{"status": "failure", "error": {"type": "authentication_fail", "desc": "Invalid or missing authentication"}}
403

This is how i deal with most api-challenges:


# Authentication with maloja
endpoint = f"{MALOJA_URL}/apis/mlj_1/newscrobble"
headers = {"Authorization": f"Bearer {MALOJA_API_KEY}"}
response = requests.get(endpoint)
pprint.pprint(response)
if response.status_code == 200:
    challenge =  response.json()["challenge"]
else:
    sys.exit()


# Generate the challenge response using API key and challenge
challenge_response = hashlib.md5(f"{MALOJA_API_KEY}{challenge}".encode()).hexdigest()
response = requests.post(endpoint, json={"key": MALOJA_API_KEY, "response": challenge_response})
if response.status_code == 200:
    token =  response.json()["token"]
else:
    sys.exit()

for track in release.tracklist:
    # Split the duration string into minutes and seconds
    minutes, seconds = map(int, track.duration.split(':'))
    # Create a timedelta object with the given minutes and seconds
    delta = timedelta(minutes=minutes, seconds=seconds)
    total_seconds = delta.total_seconds() # for scrobbling
    total_seconds = math.ceil(total_seconds) # round up for error correction

    # scrobbling
    scrobble_data = {
        "artists": track.artists,
        "title": track.title,
        "duration": total_seconds,
        "time": int(start_time.timestamp())
    }
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    start_time = start_time + delta # create new starttime for next track

    # scrobble
    response = requests.post(endpoint, json=scrobble_data, headers=headers)
@BitesizedLion
Copy link

BitesizedLion commented Apr 29, 2024

Where did you get any of this from?

There's nothing documented or mentioned anywhere about a "challenge" or using the endpoint like that.

This is how you use the endpoint:

POST /apis/mlj_1/newscrobble?key=T37Ae3yd0WLEIKSXm4aUiBWEDGcLx8eLCkBntczLM3mTs9hNTiVzQjL42J0DLK HTTP/1.1
Content-Type: application/json

{
	"artist": "Rihanna",
	"title": "Work",
	"duration": 189,
	"length": 219
}

T37Ae3yd0WLEIKSXm4aUiBWEDGcLx8eLCkBntczLM3mTs9hNTiVzQjL42J0DLK being the API key.

@timkicker
Copy link
Contributor Author

Thanks for spending your time helping me. I thought it was done using challenges as there is an endpoint in the api explorer. It seems to work now thanks to your solution thought.

@BitesizedLion
Copy link

as there is an endpoint in the api explorer

Which endpoint? There's none as far as I can see, are you on the latest version?

image

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

2 participants