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

Auth.py missing; wrong import for SpotifyOAuth #163

Open
Wattseheck opened this issue Apr 22, 2021 · 39 comments
Open

Auth.py missing; wrong import for SpotifyOAuth #163

Wattseheck opened this issue Apr 22, 2021 · 39 comments

Comments

@Wattseheck
Copy link

Wattseheck commented Apr 22, 2021

Describe the bug
Auth.py does not install when installing mycroft-spotify. When running Auth.py SpotifyOAuth is loaded from the wrong package and thus fails.

To Reproduce
Steps to reproduce the behavior:

  1. Try to run Auth.py
  2. It will fail because it cannot "from spotipy import SpotifyOAuth"

Expected behavior
Importing SpotifyOAuth

Solution

  1. SCP Auth.py manually into /opt/mycroft/skills/mycroft-spotify.forslund/
  2. Change "from spotipy import SpotifyOAuth" to "from spotipy.oauth2 import SpotifyOAuth"

Environment (please complete the following information):

  • Device type: Ras Pi 3
  • OS: Picroft
  • Mycroft-core version: Don't know

Additional context
I am sorry. This is literally my first time on GitHub and I am no developer or have any experience. Bear with me if anything is unconventional.

@Wattseheck Wattseheck added the bug label Apr 22, 2021
@forslund
Copy link
Owner

Hi, thanks for reporting :)

A couple of questions

  1. How did you install this update?

Just to verify did you run

source venv-activate.sh

from the mycroft-core folder?

Can you please run mycroft-pip show spotipy to verify that the msm install upgraded to the 2.17.1 version.

Also welcome to github, glad to have you here providing feedback and helping to improve the skill!

@Wattseheck
Copy link
Author

Hi,
I installed the spotify skill using voice. Then I installed the raspotify as in the instruction of your readme. Then I used source venv-activate.sh from the mycroft-core folder.
mycroft-pip show spotipy actually shows that I am running the version 2.4.4. Here it says https://pypi.org/project/spotipy/ latest version is 2.18. I am confused. Thanks for you help

@forslund
Copy link
Owner

Ok, the spotify skill version hasn't been updated in mycroft-skills store so the latest changes for the manual authentication hasn't been added to that version.

Try asking mycroft to

install the beta version of the spotify skill

which should update the skill to the latest version and update the requirements along with it. (can be some issues though since you've manually added files...).

Currently I think both the __init__.py and spotify.py in the skill folder have old code making it hard to run using the generated auth.

@Wattseheck
Copy link
Author

Wattseheck commented Apr 23, 2021

Asking mycroft to install the beta version did not work as mycroft asked me which version I am referring to. I did not know the answer to this.
Then I tried the following:
Deinstall the skill, reinstall the skill. Copying init.py and auth.py into the folder, made sure they replaced the old ones and match the files on GitHub, changed the requirements.txt to specify spotipy 2.17.1. Run again.

First error: from spotipy import SpotifyOAuth - "cannot import name "SpotifyOAuth" from spotipy. So I went into auth.py and changed it to from spotipy.oauth2 import SpotifyOAuth. This removed the error but threw the next one:
auth.py line 45: token_info = am.validate_token(am.cache_handler.get_cached_token()). AttributeError: "SpotifyOAuth" object has no attribute "validate_token"

Since then I am clueless.

@Jean28518
Copy link

I had the same problem.

With .venv activated I typed: pip install spotipy --upgrade Then it worked for me.

@Wattseheck
Copy link
Author

Hi, thanks this already helps a lot and brings me one step further. However, the auth then asks to "Enter the URL you were redirected to" I don't understand. I was redirected to nowhere. I entered localhost:8888 but why should this work. Also trying to connect to localhost:8888 does not work. Any idea what I am supposed to enter here?

@forslund
Copy link
Owner

spotipy should have opened up the browser or showed you a link to paste in the browser. The link would take you to spotify where you allow the application to connect to your account. Afterwards it would redirect you to a https://localhost:8888/[lots of letters and numbers] adress. That address should be pasted back into the auth script (the url contains the information needed by spotipy to create your token)

However if the beta update didn't succeed the rest of the skill won't be able to use the auth token the auth script generates.

@Jean28518 how did you update to the latest version? git pull or msm / voice?

@wieluk
Copy link

wieluk commented May 11, 2021

I have the same problem as Wattseheck ... I don't really understand the last part. I am doing this in a console via ssh so no browser will open up and also no link is shown in terminal.

@forslund
Copy link
Owner

Thanks for the feedback @wieluk, The lib should have show an url, and if this is missing for you there's a bug. I'll try to reproduce. is this picroft or other system?

@wieluk
Copy link

wieluk commented May 12, 2021

Thanks for your fast answer it is raspbian os 32bit with mycroft installed for linux

@jhthompson12
Copy link

Nothing new to report, im getting the same issue as others when running auth.py. Where things seem to go well until I am prompted to "Enter the URL you were redirected to".

From other comments it seems like the expectation is that, at this point, auth.py is supposed to open a browser window. Has anyone figured out a way to make this work / get the necessary data via an SSH connection to the picroft?

Thanks you! Im very excited to get this working :)

@forslund
Copy link
Owner

I just pushed a change to enforce showing the link in all cases. let me know if this helps.

@wieluk
Copy link

wieluk commented May 13, 2021

Thanks for the fast change. But now I get this after entering client id and secret.

Traceback (most recent call last): File "/opt/mycroft/skills/mycroft-spotify.forslund/auth.py", line 44, in <modu le> open_browser=False) TypeError: __init__() got an unexpected keyword argument 'open_browser'

I am using this "from spotipy.oauth2 import SpotifyOAuth" suggested by wattseheck instead of the one in your auth.py file because with yours i get this Error ImportError: cannot import name 'SpotifyOAuth' from 'spotipy'

@forslund
Copy link
Owner

Hmm, could you check the version of spotipy you're using? mycroft-pip show spotipy if it's not 2.17.1 try running mycroft-pip install -r /opt/mycroft/skills/mycroft-spotify.forslund/requirements.txt

(If you've updated with a git pull the version may still be the older one)

@jhthompson12
Copy link

Thanks @forslund! That definitely moved things along!

I was able to successfully get through the auth.py script with your change of open_browser=False. Now If I open Spotify on my phone I can see my Picroft device as an available device and can switch to it to play songs, which is a major improvement (though this seems to cause Mycroft to crash, which is fine for now).

Unfortunately, i am unable to control Spotify with Mycroft because I am prompted to set up my credentials at home.mycroft.ai. But this process seems to be broken too? any interactions with the username / password / connect inputs results in a new page opening with INVALID_CLIENT: Failed to get client. Do you have any suggestions about this?

@wieluk
Copy link

wieluk commented May 14, 2021

yea for me auth.py works now aswell but same problem with INVALID_CLIENT: Failed to get client.

@forslund
Copy link
Owner

You shouldn't need to connect in the skill settings. just add username and password and save (auth.py replaces the need for using the web connect flow)

@forslund
Copy link
Owner

Best test of the skill is to ask what spotify devices are available should tell you if it think's it's authorized.

@wieluk
Copy link

wieluk commented May 14, 2021

well the auth.py doesnt complain it just asks me if I want to save the client secrets i said yes. When I ask what devices are available it answers with >> Unable to authorize with the Spotify service. Please go to Skill settings at home dot mycroft dot ai to verify your username and password and to Connect to Spotify.

@forslund
Copy link
Owner

forslund commented May 14, 2021

Hmm, that is strange. as long as the username and password is set on home.mycroft.ai and auth.py is run it should be able to connect after a restart...

Edit: The user / password requirement thing on home is technically something that can be dropped now, was mainly to not spam the oauth endpoint if the user hadn't set up spotify yet...since that endpoint isn't used anymore it should be safe to just do the auth even if no username is set. Mainly Mark-1 that has use for the username and password since on that platform it runs the spotify player as well.

@forslund
Copy link
Owner

I've pushed 2 small changes now,

  1. remove the connect button from home.
  2. removed the requirement for user to be set on home

@jhthompson12
Copy link

@forslund, my person! Branch 20.08 is working for me now.

You rule!

@forslund
Copy link
Owner

Oh, sweet! Thanks for the feedback! Let me know if there are follow up issues.

@jhthompson12
Copy link

I've been playing with it a bit and it seems to be working well!

There's one minor "bug" that isnt really that big of a deal, butlet me know what you think:

Things work perfectly if i initiate the spotify session / playback with a Mycroft voice command. The bug occurs when I initiate the raspberry pi playback directly through the Spotify app (i.e. start playing music on the phone app then change the playback device to be the Picroft device). The device switch and playback works well (i think through a standalone raspotify instance that is not related to mycroft) but if i then try to control the raspberry pi playback with mycroft commands mycroft seems confused and doesnt know that a raspotify session is already in progress. Does this make sense?

So Im wondering if it's possible for mycroft to check if raspotify is already running so that it can interact with it (e.g. "pause tspotify", "turn off spotify", etc.).

Let me know if I should submit a separate issue. Thanks!

@forslund
Copy link
Owner

That is something I've left out intentionally so far, it hasn't been entirely clear when mycroft should "take control" over the playback. what you write makes sense however, if it's playing on the mycroft device it should be aware and able to control it in any case. please open a separate issue with this and we can continue to discuss the interaction there.

@wieluk
Copy link

wieluk commented May 15, 2021

thanks for me it works now too :)

edit:
oh now it doesn't ... the what spotify devices are available works but when I ask for a song or anything else than the devices I get this. Do you know why this could happen?


12:59:43.799 | INFO     | 18935 | Playback Control Skill | Resolving Player for: Play the album Hello Nasty on Spotify
12:59:47.263 | ERROR    | 18935 | mycroft.skills.mycroft_skill.mycroft_skill:on_error:835 | An error occurred while processing a request in Spotify Skill
Traceback (most recent call last):
File "/home/pi/mycroft-core/mycroft/skills/mycroft_skill/event_container.py", line 66, in wrapper handler(message)
File "/home/pi/mycroft-core/mycroft/skills/common_play_skill.py", line 96, in __handle_play_query result = self.CPS_match_query_phrase(search_phrase)
File "/opt/mycroft/skills/mycroft-spotify.forslund/__init__.py", line 435, in CPS_match_query_phrase confidence, data = self.specific_query(phrase, bonus)
File "/opt/mycroft/skills/mycroft-spotify.forslund/__init__.py", line 533, in specific_query match = re.match(self.translate_regex('podcast'), phrase,
File "/opt/mycroft/skills/mycroft-spotify.forslund/__init__.py", line 189, in translate_regex return self.regexes[regex]
KeyError: 'podcast'
Removing event mycroft-playback-control.mycroftai:PlayQueryTimeout

edit:
I think I am confused about the recommended install method of spotify skill if I do mycroft msm install github link of this repo I still get a old version I manually updated but it still doesn't play I just get PlayQueryTimeouts.

@forslund
Copy link
Owner

The traceback looks like some trouble with the translation? Are you running en-us or some other language?

msm -l install mycroft-spotify would be the safest bet (-l is for latest / ignore version submitted to mycroft-skills). Otherwise via voice hey mycroft install the beta version of the spotify skill should(TM) work.

If you did an install using the github link it may have downgraded spotipy for you. Does "what spotify devices are available" still work for you?

@Xanaxus
Copy link

Xanaxus commented Jun 11, 2021

I am still facing the issue of authorization:

Unable to authorize with the Spotify service. Please go to Skill settings at home dot mycroft dot ai  to verify your username and password and to Connect to Spotify.

Git status:

On branch 20.08
Your branch is up to date with 'origin/20.08'.

I also entered the details on Mycroft skill workshop button, I was unable to get the connect button like before.

I have done a git clone of this repo into the mycroft-core in its skills folder

Device Info:

  1. Laptop
  2. Manjaro KDE
  3. Main shell: fish but ran the python script in the bash

@forslund
Copy link
Owner

forslund commented Jun 12, 2021

@Xanaxus did you run the auth.py script successfully after doing the clone? Also you shouldn't have a Connect button to press anymore...

The complete process should be something like this (assuming the skill doesn't exist locally already. if so remove it)

cd /opt/mycroft/skills
git cone https://github.com/forslund/spotify-skill mycroft-spotify.forslund
cd [your mycroft-core dir]
source venv-activate.sh
pip install -r /opt/mycroft/skills/mycroft-spotify.forslund/requirements.txt
python /opt/mycroft/skills/mycroft-spotify.forslund/auth.py

Follow the instructions in the script and chose to save the Client Secret.

./start-mycroft.sh all

Go to the skills page on the mycroft website.

Enter your username and password then hit save. If the skill is up to date there should be no "Connect" button to press.

Now that 21.02 of core is released I should be able to push the update to the skill store removing a couple of these steps making things slightly easier.

@alexbk66
Copy link

I tried the above. When restart MyCroft I get "SpotifySkill | Couldn't fetch credentials"

@alexbk66
Copy link

alexbk66 commented Nov 11, 2021

I tried the above. When restart MyCroft I get "SpotifySkill | Couldn't fetch credentials"

Ok, I forgot source venv-activate.sh

Now I get

you have not yet setup any music skills

Note: I do have listed mycroft-spotify.forslund

@pjchamberlain
Copy link

I'm trying to get the spotify skill working on a mycroft that I've installed on a linux desktop.

I've read through the thread above and think I followed all the steps

install the beta version of the spotify skill
created a developer app on spotify

source venv-activate.sh and pip install spotipy --upgrade and run auth.py and followed all the instructions

but what I see in the log is the that the skill fails to load.

2022-12-28 16:43:21.868 | ERROR    | 1369931 | mycroft.skills.skill_loader:_load_skill_source:278 | Failed to load skill: mycroft-spotify.forslund (ImportError("cannot import name 'SpotifyO
Auth' from 'spotipy' (/home/patrick/mycroft-core/.venv/lib/python3.10/site-packages/spotipy/__init__.py)"))
Traceback (most recent call last):
  File "/home/patrick/mycroft-core/mycroft/skills/skill_loader.py", line 276, in _load_skill_source
    skill_module = load_skill_module(main_file_path, self.skill_id)
  File "/home/patrick/mycroft-core/mycroft/skills/skill_loader.py", line 72, in load_skill_module
    spec.loader.exec_module(mod)
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/opt/mycroft/skills/mycroft-spotify.forslund/__init__.py", line 39, in <module>
    from .spotify import (MycroftSpotifyCredentials, SpotifyConnect,
  File "/opt/mycroft/skills/mycroft-spotify.forslund/spotify.py", line 13, in <module>
    from .auth import AUTH_DIR, SCOPE
  File "/opt/mycroft/skills/mycroft-spotify.forslund/auth.py", line 20, in <module>
    from spotipy import SpotifyOAuth
ImportError: cannot import name 'SpotifyOAuth' from 'spotipy' (/home/patrick/mycroft-core/.venv/lib/python3.10/site-packages/spotipy/__init__.py)
2022-12-28 16:43:21.872 | ERROR    | 1369931 | mycroft.skills.skill_loader:_communicate_load_status:351 | Skill mycroft-spotify.forslund failed to load

@forslund
Copy link
Owner

forslund commented Dec 28, 2022

Hi pip install spotipy --upgrade will give you the latest version of spotipy and they've done some changes since the version this skill is using.

Try resetting the version to the expected 2.17.1 (this was installed when you asked mycroft to install the beta version)

source venv-activate.sh
pip install spotipy==2.17.1

and then try to run the auth-script start the skill.

@forslund
Copy link
Owner

I've upgraded here and I can't reproduce your issue @pjchamberlain. Could you provide the output from

pip show spotipy

and

python --version

so I can see if I can make a better reproduction of the system.

@pjchamberlain
Copy link

Thanks you've indirectly highlighted some problems with my python and pip set up .. I'm on Ubuntu 22.04 and haven't actively touched python or pip for a few years until my Mycroft Mark 2 arrived.

Fixing the issues with python and pip so that the simple commands above run without errors, and then restarting mycroft and repeating all the setup steps .. has got it working. This is with spotipy 2.22.0

Happy days. Thanks for your help. I've learned a lot about mycroft. My next puzzle is what to do with my Mark 2 ..

@forslund
Copy link
Owner

If you set up the spotify skill on the mark 2 feel free to submit fixes or updates to the readme to get it going.

@pjchamberlain
Copy link

pjchamberlain commented Dec 30, 2022

Porting skills to the Mark 2 is a bit beyond me at this point.

I hadn't looked at Mycroft code at all until my Mark 2 arrived. There is very little information about getting started on the Mark 2. I followed the guide to porting hello world, and got that working eventually.

I'm not sure what the best way forward is - replace Dinkum on my Mark 2 with something else or to try to learn how to port skills I want onto Dinkum. The mycroft ai forum has a lot of people asking the same question.

@forslund
Copy link
Owner

My Mark-2 arrived today but will likely not start experimenting with it until after new years. I was thinking of going the "sandbox" way but maybe getting it onto Dinkum is a possibility

@leogermani
Copy link

The instructions in this comment worked fine on a Docker instance of mycroft.

Any news on releasing the latest version to the store to make it simpler to install this skill?

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

9 participants