XDGFX / ultrasonics-api

API proxy for ultrasonics
GNU General Public License v3.0
15 stars 8 forks source link

How to set up redirect URIs? #1

Open Chaphasilor opened 2 years ago

Chaphasilor commented 2 years ago

I can't find documentation on this and my attempts so far didn't work out.

I've set up the API server on my server, accessible on port X. I get the default response when navigating to /api as well, so that seems to work as expected.

I've configured applications for both Spotify and Deezer on their respective dashboards and used the IDs and keys for the necessary environment variables.

Now I'm stuck at the redirect URIs. I've configured the Spotify one to :X/api/spotify/auth, which I took from here.
In that function it seems like the redirect URI is hard-coded to the heroku instance, although I could be wrong.
I did the same for deezer, and this time there was no hard-coded backlink in the auth function.

Now, when I go to ultrasonics and try to authenticate with Spotify or Deezer, I always end up with an error saying "INVALID_CLIENT: Invalid redirect URI" (for Spotify) or "You must specify a valid redirect URI" (for Deezer).
But I have a redirect URI set up on the developer dashboard for both applications.
Are IPs not allowed for these redirect URIs? Given that the client is being redirected and has access to that local IP, it should technically work, right?

Some more documentation on the matter would be really helpful if we are to migrate away form the heroku-hosted API :)

Edit: Looking at the URL that is used to send the authentication request (to Spotify's/Deezer's website), it seems like the heroku instance URI is hard-coded for both services. Could this be changed please, for example by adding an environment variable for it?

Chaphasilor commented 2 years ago

These would be the places where the environment variable is needed:

Deezer:

https://github.com/XDGFX/ultrasonics-api/blob/d1994df659193d08158bea5f6eba0d19f556b8d2/ultrasonics_api/services/deezer.py#L75

Spotify:

https://github.com/XDGFX/ultrasonics-api/blob/d1994df659193d08158bea5f6eba0d19f556b8d2/ultrasonics_api/services/spotify.py#L92

https://github.com/XDGFX/ultrasonics-api/blob/d1994df659193d08158bea5f6eba0d19f556b8d2/ultrasonics_api/services/spotify.py#L150

Chaphasilor commented 2 years ago

I've managed to patch the python files to load a base URL for the API from an environment variable and use that for the redirect URIs. Spotify authentication is now working, but for Deezer I'm getting the following error:

Access Denied
You don't have permission to access "http://connect.deezer.com/oauth/auth.php?" on this server.

My request looks like this:
https://connect.deezer.com/oauth/auth.php?app_id=<redacted>&redirect_uri=http://<IP>:<X>/api/deezer/auth&perms=basic_access,offline_access,manage_library,delete_library, which aligns with the documentation at https://developers.deezer.com/api/oauth. Any idea what I'm missing here?

Once I manage to get this working I'd be happy to create a PR for this :)

Chaphasilor commented 2 years ago

Okay, it seems like Deezer's Application domain doesn't support plain IP addresses, whereas Spotify has no problems with that.
So in order to authenticate with Deezer, you need to either use an actual domain for the API server, or use mDNS in order to replace you IP with an equivalent domain name...

Edit: it seems like mDNS resolution does not work from the ultrasonics python server / docker image, so you'll have to use the actual IP there (in the ultrasonics settings on the website), otherwise the initial auth will work, but renewing tokens will not, because for that the ultrasonics server has to make a request to the ultrasonics-api server :)

MauliQT commented 2 years ago

Hi i am trying to setup the api rn, but i cant access the spotify auth site and I don't know how to do it, and you said you got it running, could you please help me out

Chaphasilor commented 2 years ago
  1. Go here and log in with your Spotify Account: https://developer.spotify.com/dashboard/
  2. Create a new application, name it e.g. "ultrasonics"
  3. In your app's settings, set the redirect URL to the URL where your ultrasonics-api server is reachable

This might not be enough to help you out, if you still have problems I can send you some screenshots once I get back to my PC :)

MauliQT commented 2 years ago

Does it have to be a public ip or can i do it just with my local ip. And if not is it possible to reverse proxy the api through nginx Edit: tried routing it trough nginx it works, but still cant auth spotify

Screenshot 2022-11-07 at 16 36 12 Screenshot 2022-11-07 at 16 37 03 Screenshot 2022-11-07 at 17 06 19

Edit: changed Domain from music.fjcx764.uk to music.fjcx764.uk/api

Chaphasilor commented 2 years ago

Yeah local IPs should work for Spotify. What you seem to be missing is the correct path for the redirect URL, it should be http://192.168.2.124:8003/api/auth/spotify and not just http://192.168.2.124:8003 :)

MauliQT commented 2 years ago

oh with the auth/spotify aswell

MauliQT commented 2 years ago

still doesn't work, could you do some screenshots of your setup

MauliQT commented 2 years ago

Nvm works now just had to restart everything

Chaphasilor commented 2 years ago

Great!

haudankaivajasi commented 1 year ago

Okay, I'm just baffled now as I'm trying to get the self hosted ultrasonics-api to work on docker. I'm running both on my server and used the given docker commands to run the containers with api keys and secrets.

I have entered ultrasonics-api URL as: http://server_ip:8003/ in the Ultrasonics settings screen and added http://server_ip:8003/spotify/auth/request to Spotify api Redirect URIs. I also tried https://ultrasonics-api.mydomain.com/ in Ultrasonics settings and in https://ultrasonics-api.mydomain.com/spotify/auth/request in Spotify. I've restarted both Ultrasonics and the api container after changes just to be sure.

Every time I try to authenticate with Spotify it gives me a "Not found" page.

Is there a problem with the api container or what might cause this behavior?

haudankaivajasi commented 1 year ago

Okay, I'm just baffled now as I'm trying to get the self hosted ultrasonics-api to work on docker. I'm running both on my server and used the given docker commands to run the containers with api keys and secrets.

I have entered ultrasonics-api URL as: http://server_ip:8003/ in the Ultrasonics settings screen and added http://server_ip:8003/spotify/auth/request to Spotify api Redirect URIs. I also tried https://ultrasonics-api.mydomain.com/ in Ultrasonics settings and in https://ultrasonics-api.mydomain.com/spotify/auth/request in Spotify. I've restarted both Ultrasonics and the api container after changes just to be sure.

Every time I try to authenticate with Spotify it gives me a "Not found" page.

Is there a problem with the api container or what might cause this behavior?

Ok, I got it to work. First I had to add this modified spotify.py file to the container with -v /file/location//ultrasonics/api/spotify.py:/app/ultrasonics_api/services/spotify.py and also add -e SPOTIFY_REDIRECT_URI=http://my_internal_ip:8003/api/spotify/auth \ to the docker command when running the container. This way it won't redirect straight to heroku app.

Then I added http://my_internal_ip:8003/api/ to ultrasonics settings via the web interface. Lastly I added http://my_internal_ip:8003/api/spotify/auth to the Spotify app's "Redirect URIs" section.

I hope this helps someone struggling to get this to work!