forum

[Archived] Osu API Questions

posted
Total Posts
23
Topic Starter
sid_____
Hi, I am trying to use the osu API for a discord bot. I am having trouble figuring out how to retrieve a json response using requests. (I'm using python). I am also getting an error that is troubling me.

Here is the error:
"b'{"error":"The authorization grant type is not supported by the authorization server."}'"

And my code is linked here:
[redacted]

I put in some extra headers specifying the content type that I would like to get back. It worked in a youtube tutorial but not for me.

My question is: Why is the response html instead of json? And, why am I getting that error after I specify which content I would like to receive?

Sorry if this is confusing
Thanks
Death
The pastebin you included has your client secret it in, never share that. Please go to the settings page and reset it for that client.

As for the actual issue you are having, include everything you had in the headers of that request in the payload/data of the request instead.
Topic Starter
sid_____
Yeah I just changed it. oops...........
Death
Forgot to mention you may also need to change the content type too. Here is a modified version of something I wrote by the way (I modified it in pastebin so it might have some syntax errors).

https://pastebin.com/GjKHhPqT

It looks like you are kind of skipping a step though. If you want to use the authorization_code grant, you first need to post to oauth/authorize to get an authorization code, and that is what you include for the code parameter when posting to oauth/token.

docs/index.html#authorization-code-grant
Topic Starter
sid_____
I did that part. Thats why I have a code.
Death
Oh I didn't see it. Then yeah, all you should need to do is move the parameters to the body of the request and change the content type.
Topic Starter
sid_____
So I did what you suggested but I don't understand what you mean by "include everything you had in the headers of that request in the payload/data of the request instead."

My code now looks like this:

response4 = requests.post('https://osu.ppy.sh/oauth/token', headers = headers, data = data)

Is this not correct? How do I include arguments in the payload?
Death
Take a look at the pastebin that I linked.

The request looks correct, but it's really a matter of what you include in the data object and what you include in the headers object. Originally you had everything in one bit headers object, but instead you need everything (except content-type) in one big data object.
Topic Starter
sid_____
Okay I switched all the parameters to their correct places. Now I have another question.

In the docs, specifically here: [url]docs/index.html?bash#authorization-code-grant
[/url]

It shows a json object on the right side of the screen with different values that I can use. How do I obtain this json object? My code always returns html.
Death
call .json() on the response
Topic Starter
sid_____
I've tried this and I get a JSON error every time.

Code:
print((response4.json()).encode('utf-8'))
Death
Not really sure what you are doing wrong then, this is something you'll probably need to debug yourself. https://2.python-requests.org/en/master/user/quickstart/#json-response-content

If the response you are getting is still HTML, you are probably missing something in the request still or your request is malformed somehow. If that's the case I highly recommend reading the docs I linked very carefully one more time.
Topic Starter
sid_____
Sorry to keep bothering you, but the suggestion you gave me is not working. The headers and data that are passed into the request seem to not work because I am getting a 401 error when trying to connect to the website.

Here is the code: https://pastebin.com/VWdGQpj0
Death
That probably means your authorization code or some other parameter you are providing in the request is invalid.
Topic Starter
sid_____
Still not working... Does the code change every time the program runs? If so, that would be my issue...
Death
I don't know how your program works so I couldn't tell you.

You are doing something wrong on your end, but without actually seeing what you are doing it is impossible to debug.

This is a very standard OAuth2 authorization flow, it's not specific to osu!. There should be many resources online that give you explanations with examples for how to go through it properly.
GPR

sid_____ wrote:

Sorry to keep bothering you, but the suggestion you gave me is not working. The headers and data that are passed into the request seem to not work because I am getting a 401 error when trying to connect to the website.

Here is the code: https://pastebin.com/VWdGQpj0
From what I see from your code here, looks like you want to use grant type client_credentials not authorization_code.

authorization_code grant type needs user permission first so the response will be html because it's redirected to authorization page like this.



Meanwhile client_credentials grant type doesn't need user authorization and behave like 'guest' to interact with API. (because you don't need to login first)

I hope this helps you.
Topic Starter
sid_____
This did help me, but its still returning html. Could you take a look at my code? Sorry for asking so many questions...
[url]https://pastebin.com/iGKTHHPr
[/url]
Death
You put everything in the headers again, this is all supposed to be in the body.

Also just thought I'd mention

GPR wrote:

authorization_code grant type needs user permission first so the response will be html because it's redirected to authorization page like this.
The code was posting to oauth/token, not oauth/authorize, so assuming the authorization code in the request had already been generated properly and all of the other parameters were correct, that endpoint should not return HTML.

Though I do agree client_credentials is probably better unless he needs users to authenticate with his client (probably not likely for a Discord bot).
Topic Starter
sid_____
Not sure what you mean by putting things in the "body" instead of headers. Could you explain please?
Death
I should have specified, sorry. The python requests library calls it "data", but it's put into the body of the request.

https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests

And from https://requests.readthedocs.io/en/master/api/?highlight=post#requests.post
data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.
Topic Starter
sid_____
Finally it worked! Thanks so much for helping me. I might have more questions later...

EDIT: A few more questions. First off, how do I access this data?

https://osu.ppy.sh/docs/index.html#user

Also, how do I get a user's ID with only a name provided? (I would like to make a discord bot that searches for users with their name)
GPR
Try this endpoint in docs docs/index.html#apiv2usersusermode

The documentation itself is not complete yet. If you need some kind of help via discord, you can join dev discord https://discord.gg/ppy and ask in #osu-web channel
Please sign in to reply.

New reply