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

Configuration Error: "Object of type RefreshingToken is not JSON serializable" during OAuth setup #1

Open
comarquet opened this issue Nov 11, 2024 · 0 comments

Comments

@comarquet
Copy link

Description:
During the OAuth setup, the login process completes successfully, but the program fails when trying to write the configuration file.

Steps to Reproduce:

  1. Clone the YTMigrate repository.
  2. Run python main.py to initiate the setup.
  3. Follow the OAuth instructions by navigating to the provided URL and logging into the destination Google account.
  4. After successfully completing the OAuth flow, return to the terminal and press Enter.

Expected Behavior:
After completing the OAuth process, the configuration file (config.json) should be generated, enabling migration of playlists and other account data.

Actual Behavior:
After completing the OAuth flow and pressing Enter, the following error occurs when trying to write the configuration file:

Error Output:

19:01 <corentin@Corentin> ~/Downloads/YTMigrate-main (2) $ python main.py
YTMigrate, version 1.0

Configuration file not found!
Set up accounts:
Log in with Oauth for destination account:
Go to https://www.google.com/device?user_code=CZC-PYJ-ZRYM, finish the login flow and press Enter when done, Ctrl-C to abort
Writing configuration file...
Failed to create config: Object of type RefreshingToken is not JSON serializable

State of config.json after Execution:

{
  "source_account": {
    "oauth_headers": 

The configuration file remains incomplete, indicating an issue with the serialization process for the OAuth credentials.

Analysis and Root Cause:
The main.py script attempts to write the configuration details to a JSON file, but it encounters an issue when dealing with the OAuth credentials. Specifically, the object returned during OAuth login seems to be of type RefreshingToken, which is not directly serializable using the standard json.dump() method.

From reviewing main.py, it appears that the OAuth login uses the ytmusicapi.setup_oauth() function to authenticate and store credentials. This object (RefreshingToken) needs a custom serialization to be written to config.json.

The problematic part seems to be related to how the program saves the OAuth credentials after authentication:

# Hypothetical code snippet where the issue might occur
with open(config_filename, "w") as config_file:
    json.dump(auth_credentials, config_file)  # auth_credentials may be a RefreshingToken

The RefreshingToken likely contains methods or attributes that cannot be directly converted to JSON format, which results in this error.

Suggestions for Resolution:

  1. Modify the code that writes the configuration file to convert the RefreshingToken into a JSON-compatible format:

    • Implement a method like to_dict() for the RefreshingToken class to convert its attributes to a dictionary.
    • Replace:
      json.dump(auth_credentials, config_file)
      with:
      json.dump(auth_credentials.to_dict(), config_file)

    This would ensure that only JSON-serializable data is saved.

  2. Alternatively, use a more flexible serialization library, such as pickle, for saving the OAuth credentials. This can serialize Python objects more completely, though it has different security considerations.

  3. Consider adding more debug statements around the serialization step to log the attributes of RefreshingToken and understand which specific field is causing the serialization failure.

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

1 participant