[GH-ISSUE #1511] Failed to create Spirc instance: Error { kind: InvalidArgument, error: StatusCode(400) } #682

Closed
opened 2026-02-27 19:31:56 +03:00 by kerem · 5 comments
Owner

Originally created by @thedtvn on GitHub (Jun 17, 2025).
Original GitHub issue: https://github.com/librespot-org/librespot/issues/1511

There are no docs orexamplese for this, soIi created this issue

use futures_util::StreamExt as _;
use librespot::{connect::{ConnectConfig, Spirc}, core::{Session, SessionConfig}, discovery::{Credentials, DeviceType, Discovery}, playback::{audio_backend, config::{AudioFormat, PlayerConfig}, mixer::{self, MixerConfig}, player::Player}};

const APP_NAME: &str = "Spotify Player";
const DEVICE_TYPE: DeviceType = DeviceType::Tv;

async fn discover_prosessing(session: Session, credentials: Credentials) {
    let player_config = PlayerConfig::default();
    let audio_format = AudioFormat::default();
    let mut connect_config = ConnectConfig::default();
    connect_config.name = APP_NAME.to_string();
    connect_config.device_type = DEVICE_TYPE;
    let mixer_config = MixerConfig::default();
    let sink_builder = audio_backend::find(None).unwrap();
    let mixer_builder = mixer::find(None).unwrap();
    let mixer = mixer_builder(mixer_config);
    let player = Player::new(
        player_config,
        session.clone(),
        mixer.get_soft_volume(),
        move || sink_builder(None, audio_format),
    );
    let (spirc, spirc_task) =
        Spirc::new(connect_config, session.clone(), credentials, player, mixer).await
        .expect("Failed to create Spirc instance");

    spirc.activate().expect("Failed to activate Spirc");
    spirc_task.await;
}


#[tokio::main]
async fn main() {
    env_logger::init();
    let session_config = SessionConfig::default();
    let mut discovery = Discovery::builder(&session_config.device_id, &session_config.client_id)
        .name(APP_NAME)
        .device_type(DEVICE_TYPE)
        .launch()
        .expect("Failed to create discovery instance");
    while let Some(cred) = discovery.next().await {
        let session_config = session_config.clone();
        tokio::spawn(async move {
            let session = Session::new(session_config, None);
            discover_prosessing(session, cred).await;
        });
        
    }

}

Here is my code I use lib ver 0.6.0-dev full direct from github

Originally created by @thedtvn on GitHub (Jun 17, 2025). Original GitHub issue: https://github.com/librespot-org/librespot/issues/1511 There are no docs orexamplese for this, soIi created this issue ```rs use futures_util::StreamExt as _; use librespot::{connect::{ConnectConfig, Spirc}, core::{Session, SessionConfig}, discovery::{Credentials, DeviceType, Discovery}, playback::{audio_backend, config::{AudioFormat, PlayerConfig}, mixer::{self, MixerConfig}, player::Player}}; const APP_NAME: &str = "Spotify Player"; const DEVICE_TYPE: DeviceType = DeviceType::Tv; async fn discover_prosessing(session: Session, credentials: Credentials) { let player_config = PlayerConfig::default(); let audio_format = AudioFormat::default(); let mut connect_config = ConnectConfig::default(); connect_config.name = APP_NAME.to_string(); connect_config.device_type = DEVICE_TYPE; let mixer_config = MixerConfig::default(); let sink_builder = audio_backend::find(None).unwrap(); let mixer_builder = mixer::find(None).unwrap(); let mixer = mixer_builder(mixer_config); let player = Player::new( player_config, session.clone(), mixer.get_soft_volume(), move || sink_builder(None, audio_format), ); let (spirc, spirc_task) = Spirc::new(connect_config, session.clone(), credentials, player, mixer).await .expect("Failed to create Spirc instance"); spirc.activate().expect("Failed to activate Spirc"); spirc_task.await; } #[tokio::main] async fn main() { env_logger::init(); let session_config = SessionConfig::default(); let mut discovery = Discovery::builder(&session_config.device_id, &session_config.client_id) .name(APP_NAME) .device_type(DEVICE_TYPE) .launch() .expect("Failed to create discovery instance"); while let Some(cred) = discovery.next().await { let session_config = session_config.clone(); tokio::spawn(async move { let session = Session::new(session_config, None); discover_prosessing(session, cred).await; }); } } ``` Here is my code I use lib ver 0.6.0-dev full direct from github
kerem closed this issue 2026-02-27 19:31:56 +03:00
Author
Owner

@photovoltex commented on GitHub (Jun 17, 2025):

I will take a look later at your provided example. Just a thing that seems off about you async code. You spawn a new thread, but don't await the handle, meaning the thread will not be executed (if I'm correct?). Couldn't you remove the tokio::spawn and just run the spirc task on the current thread? I might be wrong but maybe that all it is.

<!-- gh-comment-id:2979237889 --> @photovoltex commented on GitHub (Jun 17, 2025): - [all examples](https://github.com/librespot-org/librespot/tree/dev/examples) - [spirc example](https://github.com/librespot-org/librespot/blob/dev/examples/play_connect.rs) - [connect docs](https://github.com/librespot-org/librespot/blob/dev/connect/README.md) - you should be able to get the full documentation where the connect module should have full coverage by running the following in root or the connect directory ``` cargo doc --open ``` - taking a look at the [main loop of the binary](https://github.com/librespot-org/librespot/blob/dev/src/main.rs#L1970) might also be helpful I will take a look later at your provided example. Just a thing that seems off about you async code. You spawn a new thread, but don't await the handle, meaning the thread will not be executed (if I'm correct?). Couldn't you remove the `tokio::spawn` and just run the spirc task on the current thread? I might be wrong but maybe that all it is.
Author
Owner

@thedtvn commented on GitHub (Jun 17, 2025):

I will take a look later at your provided example. Just a thing that seems off about you async code. You spawn a new thread, but don't await the handle, meaning the thread will not be executed (if I'm correct?). Couldn't you remove the tokio::spawn and just run the spirc task on the current thread? I might be wrong but maybe that all it is.

thanks

<!-- gh-comment-id:2979243300 --> @thedtvn commented on GitHub (Jun 17, 2025): > * [all examples](https://github.com/librespot-org/librespot/tree/dev/examples) > * [spirc example](https://github.com/librespot-org/librespot/blob/dev/examples/play_connect.rs) > * [connect docs](https://github.com/librespot-org/librespot/blob/dev/connect/README.md) > > * you should be able to get the full documentation where the connect module should have full coverage by running the following in root or the connect directory > > ``` > cargo doc --open > ``` > * taking a look at the [main loop of the binary](https://github.com/librespot-org/librespot/blob/dev/src/main.rs#L1970) might also be helpful > > I will take a look later at your provided example. Just a thing that seems off about you async code. You spawn a new thread, but don't await the handle, meaning the thread will not be executed (if I'm correct?). Couldn't you remove the `tokio::spawn` and just run the spirc task on the current thread? I might be wrong but maybe that all it is. thanks
Author
Owner

@thedtvn commented on GitHub (Jun 17, 2025):

@photovoltex , can I ask what I do wrong that makes it send a 400 error (I try to connect using Spotify client connect to this app )

<!-- gh-comment-id:2979260857 --> @thedtvn commented on GitHub (Jun 17, 2025): @photovoltex , can I ask what I do wrong that makes it send a 400 error (I try to connect using Spotify client connect to this app )
Author
Owner

@photovoltex commented on GitHub (Jun 17, 2025):

I tried your provided code and connect to the connect device via the desktop player, which seemed to work. Please provide a log with your error, otherwise we can't help you with your problem.

<!-- gh-comment-id:2980804534 --> @photovoltex commented on GitHub (Jun 17, 2025): I tried your provided code and connect to the connect device via the desktop player, which seemed to work. Please provide a log with your error, otherwise we can't help you with your problem.
Author
Owner

@thedtvn commented on GitHub (Jun 17, 2025):

i fixed that thanks for sory about wasted your times

<!-- gh-comment-id:2980814788 --> @thedtvn commented on GitHub (Jun 17, 2025): i fixed that thanks for sory about wasted your times
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/librespot#682
No description provided.