6 releases
0.1.5 | Jul 13, 2024 |
---|---|
0.1.4 | Jun 30, 2024 |
#731 in Authentication
119 downloads per month
78KB
1.5K
SLoC
Google API Client
An unoffical client library for Google APIs.
crate.io: google-api-rust-client-unoffical
Installation
Run the following Cargo command in your project directory:
cargo add google-api-rust-client-unoffical
Or add the following line to your Cargo.toml:
google-api-rust-client-unoffical = "0.1.5"
Authorization
Obtain Service Account credentials or API Key from APIs & Services > Credentials in the Google Cloud Console.
Service Account
Create Credentials From JSON File
let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
Create Credentials From JSON String
let credentials_json = serde_json::json!({
"type": "service_account",
"project_id": "xxx",
"private_key_id": "xxx",
"private_key": "-----BEGIN PRIVATE KEY-----\nsome_key\n-----END PRIVATE KEY-----\n",
"client_email": "xxx@example.com",
"client_id": "xxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxx",
"universe_domain": "googleapis.com"
}).to_string();
let credentials = ServiceAccountCredentials::from_service_account_info(credentials_json)?;
Setting Scopes
let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
Setting Subject
let subjected_crentials = credentials.with_subject("itsuki@example.com");
Fetching Access Token Directly
let token = credentials.get_access_token().await?;
Using Credentials with Services
let mut translation_service = TranslateService::new_with_credentials(credentials);
let response = translation_service.list_languages(Some("ja"), None).await?;
API KEY
To use API keys, pass them in when creating service objects.
let api_key = env::var("API_KEY")?;
let mut translation_service = TranslateService::new_with_api_key(api_key);
let response = translation_service.list_languages(Some("ja"), None).await?;
Refer to auth.rs for authorization example.
Services
Cloud Translation Basic Edition (v2)
Supported APIs
- Translate: Translates input text, returning translated text.
- Detect: Detects the language of texts.
- Languages: Returns a list of supported languages for translation.
Refer to translate_service.rs for usage examples.
Routes API
Supported APIs
-
Compute Route: Get a route between a start and an end point for different modes of transportation, such as walking, biking, or different types of vehicles. You can also request route details such as distance, estimated time to navigate the route, expected tolls, and step-by-step instructions to navigate the route.
-
Compute Route Matrix: Get a matrix of information for recommended routes between multiple start and end points, including distance and estimated arrival times.
Refer to route_service.rs for usage examples.
Other Services Under Construction!
Dependencies
~13–25MB
~490K SLoC