osu-api icon indicating copy to clipboard operation
osu-api copied to clipboard

Test api key

Open dominikheinz opened this issue 7 years ago • 3 comments

Is there a way to specifically testing the API key without querying maps or user information etc? Something which just returns true or false whether a key is valid. E.g. https://osu.ppy.sh/p/api/testkey.php?key=12321432313123213123.

dominikheinz avatar Feb 03 '18 21:02 dominikheinz

If you try to request anything with an invalid key, it'll return: {"error":"Please provide a valid API key."}, instead of the data. So, I think you could just handle that in your code.

Eb3rM5 avatar Feb 05 '18 15:02 Eb3rM5

To add to @Eb3rM5 's point, the HTTP status code is 401 instead of 200, so you could also check for that. https://osu.ppy.sh/api/get_user?k=a1b2c3d4e5f6g7h8a1b2c3d4e5f6g7h8 is my go-to check.

gamescom15 avatar Mar 26 '18 07:03 gamescom15

For anyone who's reading this now, a simple solution would be to check if there's a 401 response (adding to @gamescom15)

Example using Node.js request module

request
    .get('https://osu.ppy.sh/api/get_user?k=INVALIDKEYHERE', 
    function(err, response, body) {
        // With this if statement do whatever you have to do to handle error
        if(response == 401) console.log("No valid API key")
    })

ghost avatar Jul 08 '18 03:07 ghost