rustgoogleoauth2

The library supports the following flows:

toml [dependencies] gauth = "0.4.0"

OAuth2

  1. Create your application in Google API Console
    a. Credentials > Create credentials > OAuth client ID
    b. Set application type to Other
    c. Enter your application name
    d. Download JSON configuration of newly created application

Sample client implementation

```rust,norun fn main() { // define consent URL handler // returns an auth. code which is then exchanged against access token let handleauth = |consenturl: String| -> Result> { println!("> open the link in browser\n\n{}\n", consenturl); println!("> enter the auth. code\n");

    let mut auth_code = String::new();
    io::stdin().read_line(&mut auth_code)?;

    Ok(auth_code)
}

let auth_client = gauth::Auth::new(
    "my-new-application",
    &[
        "https://www.googleapis.com/auth/drive.readonly",
    ],
    PathBuf::from("/my-google-credentials/oauth-credentials.json"),
);

let token = auth_client
    .access_token(handle_auth)
    .expect("failed to retrieve access token");

println!("obtained token: {:?}", token);

} ```

Service Account

Follow instructions for creating a service account. After a service account key has been created, it can be used to obtain an access token.

```rust,norun use gauth::servaccount::ServiceAccount;

fn accesstoken() { let scopes = vec!["https://www.googleapis.com/auth/drive"]; let keypath = "test_fixtures/service-account-key.json";

let mut service_account = ServiceAccount::from_file(key_path, scopes);
let access_token = service_account.access_token().unwrap();

println!("access token {}:", access_token);

} ```

License

License under either or: