rust-lang / git2-rs

libgit2 bindings for Rust
https://docs.rs/git2
Apache License 2.0
1.64k stars 380 forks source link

Request failed with status code: 401 #965

Open JasonMo1 opened 1 year ago

JasonMo1 commented 1 year ago

I have fill all of the values, I want to know what's wrong with these code. OS: Windows 11 rustc: 1.70.0 (90c541806 2023-05-31) git2: 0.17.2

My code:

fn push_repo() -> Result<(), git2::Error> {
    let repo = Repository::open("cache/repo")?; 

    let mut remote = repo.find_remote("origin")?;

    remote.connect_auth(Direction::Push, Some(create_callbacks()), None).unwrap();
    repo.remote_add_push("origin", "refs/heads/temp:refs/heads/temp").unwrap();
    let mut push_options = PushOptions::default();
    let callbacks = create_callbacks();
    push_options.remote_callbacks(callbacks);
    remote.push(&["refs/heads/temp:refs/heads/temp"], Some(&mut push_options)).unwrap();

    std::mem::drop(remote);

    Ok(())
}
fn create_callbacks<'a>() -> RemoteCallbacks<'a>{
    let mut callbacks = RemoteCallbacks::new();
    callbacks.credentials(|_url, username_from_url, _allowed_types| {
        // Use the username from the url or a default one
        let username = username_from_url.unwrap_or("git");
        Cred::ssh_key(
            username,
            Some(std::path::Path::new("ssh_key/pushzig_rsa.pub")),
            std::path::Path::new("ssh_key/pushzig_rsa"),
            None,
        )
    });
    callbacks
}

Error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { code: -1, klass: 34, message: "request failed with status code: 401" }', src\main.rs:261:74
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\zos-index-generator.exe new` (exit code: 101)
eyolas commented 4 months ago

Same error for me