pub struct ServiceAccountCredentials { /* private fields */ }Implementations§
Source§impl ServiceAccountCredentials
impl ServiceAccountCredentials
Sourcepub fn from_service_account_file(filepath: PathBuf) -> Result<Self>
pub fn from_service_account_file(filepath: PathBuf) -> Result<Self>
Create ServiceAccountCredentials from file.
filepath- File path to the service account credential file. File should be valid JSON.
Examples found in repository?
examples/translate_service.rs (line 35)
24async fn translate() -> Result<()> {
25 dotenv().ok();
26
27 // api auth
28 let api_key = env::var("API_KEY")?;
29 let mut translation_service = TranslateService::new_with_api_key(api_key);
30 let response = translation_service.translate(vec!["test"], "ja", None).await?;
31 println!("response: {}", serde_json::to_string(&response)?);
32
33 // service account auth
34 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
35 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
36 let mut translation_service = TranslateService::new_with_credentials(credentials);
37 let response = translation_service.translate(vec!["test"], "ja", None).await?;
38 println!("response: {}", serde_json::to_string(&response)?);
39 let mut params: HashMap<String, Value> = HashMap::new();
40 params.insert("format".to_string(), "text".into());
41 params.insert("source".to_string(), "en".into());
42 params.insert(" model".to_string(), "base".into());
43 let response_with_params = translation_service.translate(vec!["test2"], "ja", Some(params)).await?;
44 println!("response_with_params: {}", serde_json::to_string(&response_with_params)?);
45
46 Ok(())
47}
48
49
50async fn list_languages() -> Result<()> {
51 dotenv().ok();
52
53 // api auth
54 let api_key = env::var("API_KEY")?;
55 let mut translation_service = TranslateService::new_with_api_key(api_key);
56 let response = translation_service.list_languages(Some("ja"), None).await?;
57 println!("response: {}", serde_json::to_string(&response)?);
58
59 // service account auth
60 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
61 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
62 let mut translation_service = TranslateService::new_with_credentials(credentials);
63 let response = translation_service.list_languages(Some("ja"), None).await?;
64 println!("response: {}", serde_json::to_string(&response)?);
65
66 Ok(())
67}
68
69
70async fn detect_language() -> Result<()> {
71 dotenv().ok();
72
73 // api auth
74 let api_key = env::var("API_KEY")?;
75 let mut translation_service = TranslateService::new_with_api_key(api_key);
76 let response = translation_service.detect_language(vec!["test", "テスト"]).await?;
77 println!("response: {}", serde_json::to_string(&response)?);
78
79 // service account auth
80 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
81 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
82 let mut translation_service = TranslateService::new_with_credentials(credentials);
83 let response = translation_service.detect_language(vec!["test", "テスト"]).await?;
84 println!("response: {}", serde_json::to_string(&response)?);
85
86 Ok(())
87}More examples
examples/auth.rs (line 10)
6async fn main() -> Result<()> {
7
8 // service account credentials from file
9 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
10 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
11 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
12 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
13 let token = subjected_crentials.get_access_token().await?;
14 println!("token: {}", token);
15
16 // service account credentials from json
17 let credentials_json = serde_json::json!({
18 "type": "service_account",
19 "project_id": "xxx",
20 "private_key_id": "xxx",
21 "private_key": "-----BEGIN PRIVATE KEY-----\nsome_key\n-----END PRIVATE KEY-----\n",
22 "client_email": "xxx@example.com",
23 "client_id": "xxx",
24 "auth_uri": "https://accounts.google.com/o/oauth2/auth",
25 "token_uri": "https://oauth2.googleapis.com/token",
26 "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
27 "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxx",
28 "universe_domain": "googleapis.com"
29 }).to_string();
30 let credentials = ServiceAccountCredentials::from_service_account_info(credentials_json)?;
31 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
32 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
33 let token = subjected_crentials.get_access_token().await?;
34 println!("token: {}", token);
35
36 Ok(())
37}examples/route_service.rs (line 55)
23async fn get_route() -> Result<()> {
24 dotenv().ok();
25
26 // api auth
27 let api_key = env::var("API_KEY")?;
28 let mut route_service = RouteService::new_with_api_key(api_key);
29
30 let masks = vec!["routes.duration", "routes.distanceMeters"];
31 let origin = WayPoint::new_from_location(Location::new(37.419734, -122.0827784, None), None)?;
32 let destination = WayPoint::new_from_location(Location::new(35.419734, -100.0827784, None), None)?;
33
34 let mut mid_waypoint_option: HashMap<String, Value> = HashMap::new();
35 mid_waypoint_option.insert("via".to_string(), true.into());
36
37 let mut route_option: HashMap<String, Value> = HashMap::new();
38 // let mid_waypoint = WayPoint::new_from_place_id("ChIJgUbEo8cfqokR5lP9_Wh_DaM", Some(mid_waypoint_option))?;
39 // route_option.insert("intermediates".to_owned(), serde_json::to_value(vec![mid_waypoint.clone()])?);
40 route_option.insert("travelMode".to_owned(), serde_json::to_value(RouteTravelMode::Drive)?);
41 route_option.insert("routingPreference".to_owned(), serde_json::to_value(RoutingPreference::TrafficAwareOptimal)?);
42 // route_option.insert("departureTime".to_owned(), serde_json::to_value(Utc::now() + Duration::seconds(60 * 60 * 24))?);
43 // let route_modifiers = RouteModifiers::new(Some(true), Some(true), Some(true), None, None, None);
44 // route_option.insert("routeModifiers".to_owned(), serde_json::to_value(route_modifiers)?);
45 // route_option.insert("requestedReferenceRoutes".to_owned(), serde_json::to_value(vec![ReferenceRoute::FuelEfficient])?);
46 let transit_preference = TransitPreferences::new(Some(vec![TransitTravelMode::Bus]), None);
47 route_option.insert("transitPreferences".to_owned(), serde_json::to_value(transit_preference)?);
48
49 let response = route_service.get_route(&origin, &destination, Some(masks.clone()), Some(route_option.clone())).await?;
50 println!("response: {}", serde_json::to_string_pretty(&response)?);
51
52
53 // service account auth
54 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
55 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
56 let mut route_service = RouteService::new_with_credentials(credentials);
57 let response = route_service.get_route(&origin, &destination, Some(masks.clone()), Some(route_option.clone())).await?;
58 println!("response: {}", serde_json::to_string(&response)?);
59
60 Ok(())
61}
62
63
64async fn get_route_matrix() -> Result<()> {
65 dotenv().ok();
66
67 // api auth
68 let api_key = env::var("API_KEY")?;
69 let mut route_service = RouteService::new_with_api_key(api_key);
70
71 let masks = vec!["originIndex", "destinationIndex", "status", "condition", "distanceMeters", "duration"];
72 let origin = WayPoint::new_from_location(Location::new(37.419734, -122.0827784, None), None)?;
73 let destination = WayPoint::new_from_location(Location::new(35.419734, -100.0827784, None), None)?;
74
75 let mut route_option: HashMap<String, Value> = HashMap::new();
76 // let mid_waypoint = WayPoint::new_from_place_id("ChIJgUbEo8cfqokR5lP9_Wh_DaM", Some(mid_waypoint_option))?;
77 // route_option.insert("intermediates".to_owned(), serde_json::to_value(vec![mid_waypoint.clone()])?);
78 route_option.insert("travelMode".to_owned(), serde_json::to_value(RouteTravelMode::Drive)?);
79
80 let response = route_service.get_route_matrix(&vec![RouteMatrixOrigin::new(&origin)], &vec![RouteMatrixOrigin::new(&destination)], Some(masks.clone()), Some(route_option.clone())).await?;
81 println!("response: {}", serde_json::to_string_pretty(&response)?);
82
83
84 // service account auth
85 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
86 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
87 let mut route_service = RouteService::new_with_credentials(credentials);
88 let response = route_service.get_route_matrix(&vec![RouteMatrixOrigin::new(&origin)], &vec![RouteMatrixOrigin::new(&destination)], Some(masks.clone()), Some(route_option.clone())).await?;
89 println!("response: {}", serde_json::to_string_pretty(&response)?);
90
91 Ok(())
92}Sourcepub fn from_service_account_info(credentials_json: String) -> Result<Self>
pub fn from_service_account_info(credentials_json: String) -> Result<Self>
Create ServiceAccountCredentials from json string.
credentials_json- Json string of the service account crendentials.
Examples found in repository?
examples/auth.rs (line 30)
6async fn main() -> Result<()> {
7
8 // service account credentials from file
9 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
10 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
11 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
12 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
13 let token = subjected_crentials.get_access_token().await?;
14 println!("token: {}", token);
15
16 // service account credentials from json
17 let credentials_json = serde_json::json!({
18 "type": "service_account",
19 "project_id": "xxx",
20 "private_key_id": "xxx",
21 "private_key": "-----BEGIN PRIVATE KEY-----\nsome_key\n-----END PRIVATE KEY-----\n",
22 "client_email": "xxx@example.com",
23 "client_id": "xxx",
24 "auth_uri": "https://accounts.google.com/o/oauth2/auth",
25 "token_uri": "https://oauth2.googleapis.com/token",
26 "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
27 "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxx",
28 "universe_domain": "googleapis.com"
29 }).to_string();
30 let credentials = ServiceAccountCredentials::from_service_account_info(credentials_json)?;
31 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
32 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
33 let token = subjected_crentials.get_access_token().await?;
34 println!("token: {}", token);
35
36 Ok(())
37}Sourcepub fn with_scopes(&self, scopes: Vec<&str>) -> Self
pub fn with_scopes(&self, scopes: Vec<&str>) -> Self
Add scopes to request the access token for.
scopes- Scopes that your application needs access to. OAuth 2.0 Scopes
Examples found in repository?
examples/auth.rs (line 11)
6async fn main() -> Result<()> {
7
8 // service account credentials from file
9 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
10 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
11 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
12 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
13 let token = subjected_crentials.get_access_token().await?;
14 println!("token: {}", token);
15
16 // service account credentials from json
17 let credentials_json = serde_json::json!({
18 "type": "service_account",
19 "project_id": "xxx",
20 "private_key_id": "xxx",
21 "private_key": "-----BEGIN PRIVATE KEY-----\nsome_key\n-----END PRIVATE KEY-----\n",
22 "client_email": "xxx@example.com",
23 "client_id": "xxx",
24 "auth_uri": "https://accounts.google.com/o/oauth2/auth",
25 "token_uri": "https://oauth2.googleapis.com/token",
26 "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
27 "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxx",
28 "universe_domain": "googleapis.com"
29 }).to_string();
30 let credentials = ServiceAccountCredentials::from_service_account_info(credentials_json)?;
31 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
32 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
33 let token = subjected_crentials.get_access_token().await?;
34 println!("token: {}", token);
35
36 Ok(())
37}Sourcepub fn with_subject(&self, subject: &str) -> Self
pub fn with_subject(&self, subject: &str) -> Self
Add subject to grants your application delegated access to a resource.
sub- The email address of the user for which the application is requesting delegated access. Ensure that the service account is authorized in the Domain-wide delegation page of the Admin console for the user in the sub claim
Examples found in repository?
examples/auth.rs (line 12)
6async fn main() -> Result<()> {
7
8 // service account credentials from file
9 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
10 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
11 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
12 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
13 let token = subjected_crentials.get_access_token().await?;
14 println!("token: {}", token);
15
16 // service account credentials from json
17 let credentials_json = serde_json::json!({
18 "type": "service_account",
19 "project_id": "xxx",
20 "private_key_id": "xxx",
21 "private_key": "-----BEGIN PRIVATE KEY-----\nsome_key\n-----END PRIVATE KEY-----\n",
22 "client_email": "xxx@example.com",
23 "client_id": "xxx",
24 "auth_uri": "https://accounts.google.com/o/oauth2/auth",
25 "token_uri": "https://oauth2.googleapis.com/token",
26 "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
27 "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxx",
28 "universe_domain": "googleapis.com"
29 }).to_string();
30 let credentials = ServiceAccountCredentials::from_service_account_info(credentials_json)?;
31 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
32 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
33 let token = subjected_crentials.get_access_token().await?;
34 println!("token: {}", token);
35
36 Ok(())
37}Sourcepub async fn get_access_token(&mut self) -> Result<String>
pub async fn get_access_token(&mut self) -> Result<String>
Get an access token for the service account using the scopes and subject specified.
Examples found in repository?
examples/auth.rs (line 13)
6async fn main() -> Result<()> {
7
8 // service account credentials from file
9 let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
10 let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
11 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
12 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
13 let token = subjected_crentials.get_access_token().await?;
14 println!("token: {}", token);
15
16 // service account credentials from json
17 let credentials_json = serde_json::json!({
18 "type": "service_account",
19 "project_id": "xxx",
20 "private_key_id": "xxx",
21 "private_key": "-----BEGIN PRIVATE KEY-----\nsome_key\n-----END PRIVATE KEY-----\n",
22 "client_email": "xxx@example.com",
23 "client_id": "xxx",
24 "auth_uri": "https://accounts.google.com/o/oauth2/auth",
25 "token_uri": "https://oauth2.googleapis.com/token",
26 "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
27 "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxx",
28 "universe_domain": "googleapis.com"
29 }).to_string();
30 let credentials = ServiceAccountCredentials::from_service_account_info(credentials_json)?;
31 let scoped_credentials = credentials.with_scopes(vec!["https://www.googleapis.com/auth/cloud-translation"]);
32 let mut subjected_crentials = scoped_credentials.with_subject("itsuki@example.com");
33 let token = subjected_crentials.get_access_token().await?;
34 println!("token: {}", token);
35
36 Ok(())
37}Trait Implementations§
Source§impl Clone for ServiceAccountCredentials
impl Clone for ServiceAccountCredentials
Source§fn clone(&self) -> ServiceAccountCredentials
fn clone(&self) -> ServiceAccountCredentials
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ServiceAccountCredentials
impl Debug for ServiceAccountCredentials
Source§impl<'de> Deserialize<'de> for ServiceAccountCredentials
impl<'de> Deserialize<'de> for ServiceAccountCredentials
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for ServiceAccountCredentials
impl RefUnwindSafe for ServiceAccountCredentials
impl Send for ServiceAccountCredentials
impl Sync for ServiceAccountCredentials
impl Unpin for ServiceAccountCredentials
impl UnwindSafe for ServiceAccountCredentials
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more