github_auth

Crates.iogithub_auth
lib.rsgithub_auth
version0.8.0
sourcesrc
created_at2018-05-03 13:07:31.755149
updated_at2020-09-27 15:31:05.439316
descriptionAuthenticate with GitHub from the command line.
homepage
repositoryhttps://github.com/yoshuawuyts/github_auth
max_upload_size
id63561
size96,532
Yosh (yoshuawuyts)

documentation

https://docs.rs/github_auth

README

github_auth

crates.io version build status downloads docs.rs docs

Authenticate with GitHub from the command line. Caches the authentication token so that future interactions just work.

Usage

extern crate github_auth;

use github_auth::{Authenticator, Scope};

let auth = Authenticator::builder("github_auth main example".into())
  .scope(Scope::PublicRepo)
  .build();

let token = auth.auth().unwrap();
println!("{:?}", token);

let location = auth.location();
println!("Token stored at: {:?}", location);

Example Output

This dialog is only required to generate a valid token. Once a valid token is created, it will no longer be shown.

GitHub username: my_name
GitHub password:
GitHub OTP (optional): 5678

Authenticating with the token

Once you've acquired an access token, you can use it to authenticate. Here's how to authenticate with the reqwest crate.

extern crate github_auth;
extern crate reqwest;

use github_auth::Authenticator;
use reqwest::{
  header::{Authorization, Headers, UserAgent},
  Client,
};

let auth = Authenticator::new("my_example_app");
let token = auth.auth().unwrap();

let mut headers = Headers::new();
headers.set(Authorization(format!("token {}", token.as_str())).to_owned());
headers.set(UserAgent::new("my_app"));

let url = "https://api.github.com/user";
let mut res = client.get(&url).headers(headers).send()?;
println!("{:?}", res.status());

Installation

$ cargo add github_auth

License

MIT OR Apache-2.0

Commit count: 41

cargo fmt