github_auth 0.7.1

Authenticate with GitHub from the command line.
Documentation

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