Remove very outdated service_account example

This commit is contained in:
Lewin Bormann
2022-10-27 20:47:38 +02:00
parent 7c9c92163c
commit 78b79cf92c
5 changed files with 1 additions and 246 deletions

View File

@@ -60,7 +60,7 @@ webbrowser = "0.5"
hyper-rustls = "0.23"
[workspace]
members = ["examples/test-installed/", "examples/test-svc-acct/", "examples/test-device/", "examples/service_account", "examples/drive_example", "examples/test-adc"]
members = ["examples/test-installed/", "examples/test-svc-acct/", "examples/test-device/", "examples/test-adc"]
[package.metadata.docs.rs]
all-features = true

View File

@@ -1,16 +0,0 @@
[package]
name = "service_account"
version = "0.1.0"
authors = ["Lewin Bormann <lewin@lewin-bormann.info>"]
edition = "2018"
[dependencies]
base64 = "0.2"
# We can't just use the local version (../../), as google-pubsub1 will have a different
# version of yup-oauth2, leading to type errors. For testing changes locally, download
# github.com/Byron/google-apis-rs, and use the local yup-oauth2 crate in the google-pubsub1
# crate as well as here.
yup-oauth2 = "^ 1.0"
google-pubsub1 = "^ 1.0"
hyper = "0.10"
hyper-native-tls = "0.3"

View File

@@ -1,8 +0,0 @@
# `service_account` example
NOTE that this example uses yup-oauth2 v1. To see how the new asynchronous
versions (>4) can be used, please take a look at
[async-google-apis](https://github.com/dermesser/async-google-apis) and the
examples linked from there.
The API has changed a lot!

View File

@@ -1,12 +0,0 @@
{
"type": "service_account",
"project_id": "sanguine-rhythm-105020",
"private_key_id": "bbd866b207d9081b56d6cdce09782bcc5cc7b55b",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCgSvDJJ+r88GO9\nJgAserwS+T+BL7BKHjR9EWi6AaTkIMbfbR9juDa0hAtgEdW68Qmdo1ABLtjITzXM\nE2hR4Q00Q3QV+kZztfpXOyBcARzI0oUG33EY9BOsXU4fAhIMEjGj9wP2YUlPIEYt\n6CGarRCJoTykITvFL5ERtt/7yHKdcmC1XqdnHz1loJokoLixrWIQ9vHYFnlAGXN8\n4HB63GIdb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+zNXtgSHCKNbhxKKpij1FBiMBZvOtMtDVv9KfNycJoIWmP0\nemDu5K4u6x08r27RW6xPRZSZLnAydVEmtU9M+4Vxij6xDoebly/aMd4ig6M6Nq4d\n3VMHcbqcLckhG/4jngijpitzvtH4i/fxcm9t0p0CgYEAukb0W9DYuoG/OHTKlUIv\nKr0bxLv6rRk5g7OoKD5D+QiWlXQ2RXeBlXPPkR4+N/WuxxQsJ/zlOi2iKM0lB+jG\nwwPCo/WtXm5WNi4gSVZIeOtIZS+e/2QeStc/WPgVhJrZjQ118/ORaeDoVo77lkEd\nvBj5QAMZDCn/3qeOpGJ1NJo=\n-----END PRIVATE KEY-----\n",
"client_email": "pubsub-1@sanguine-rhythm-105020.iam.gserviceaccount.com",
"client_id": "117880612788657019000",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/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/pubsub-1%40sanguine-rhythm-105020.iam.gserviceaccount.com"
}

View File

@@ -1,209 +0,0 @@
//! Warning: This example is outdated. It worked with yup-oauth2 version 1. To use the new
//! asynchronous yup-oauth2, take a look at: https://github.com/dermesser/async-google-apis.
//!
//! Demonstrates the use of service accounts and the Google Cloud Pubsub API.
//!
//! Run this binary as .../service_account pub 'your message' in order to publish messages,
//! and as .../service_account sub in order to subscribe to those messages. This will look like the
//! following:
//!
//! ```
//! $ target/debug/service_account pub 'Hello oh wonderful world' &
//! $ target/debug/service_account sub
//! Published message #95491011619126
//! message <95491011619126> 'Hello oh wonderful world' at 2016-09-21T20:04:47.040Z
//! Published message #95491011620879
//! message <95491011620879> 'Hello oh wonderful world' at 2016-09-21T20:04:49.086Z
//! Published message #95491011622600
//! message <95491011622600> 'Hello oh wonderful world' at 2016-09-21T20:04:51.132Z
//! Published message #95491011624393
//! message <95491011624393> 'Hello oh wonderful world' at 2016-09-21T20:04:53.187Z
//! Published message #95491011626206
//! message <95491011626206> 'Hello oh wonderful world' at 2016-09-21T20:04:55.233Z
//! ```
//!
//! (note that this program won't work as-is, because the credentials are invalid; it will work if
//! you supply a valid client secret)
//!
//! Copyright (c) 2016 Google, Inc. (Lewin Bormann <lbo@spheniscida.de>)
//!
use std::env;
use std::thread;
use std::time;
use google_pubsub1::{self as pubsub, Subscription, Topic};
use hyper::net::HttpsConnector;
use hyper_native_tls::NativeTlsClient;
use yup_oauth2 as oauth;
// The prefixes are important!
const SUBSCRIPTION_NAME: &'static str =
"projects/sanguine-rhythm-105020/subscriptions/rust_authd_sub_1";
const TOPIC_NAME: &'static str = "projects/sanguine-rhythm-105020/topics/topic-01";
type PubsubMethods<'a> =
pubsub::ProjectMethods<'a, hyper::Client, oauth::ServiceAccountAccess<hyper::Client>>;
// Verifies that the topic TOPIC_NAME exists, or creates it.
fn check_or_create_topic(methods: &PubsubMethods) -> Topic {
let result = methods.topics_get(TOPIC_NAME).doit();
if result.is_err() {
println!("Assuming topic doesn't exist; creating topic");
let topic = pubsub::Topic {
name: Some(TOPIC_NAME.to_string()),
labels: None,
..Default::default()
};
let result = methods.topics_create(topic, TOPIC_NAME).doit().unwrap();
result.1
} else {
result.unwrap().1
}
}
fn check_or_create_subscription(methods: &PubsubMethods) -> Subscription {
// check if subscription exists
let result = methods.subscriptions_get(SUBSCRIPTION_NAME).doit();
if result.is_err() {
println!("Assuming subscription doesn't exist; creating subscription");
let sub = pubsub::Subscription {
topic: Some(TOPIC_NAME.to_string()),
ack_deadline_seconds: Some(30),
name: Some(SUBSCRIPTION_NAME.to_string()),
..Default::default()
};
let (_resp, sub) = methods
.subscriptions_create(sub, SUBSCRIPTION_NAME)
.doit()
.unwrap();
sub
} else {
result.unwrap().1
}
}
fn ack_message(methods: &PubsubMethods, id: String) {
let request = pubsub::AcknowledgeRequest {
ack_ids: Some(vec![id]),
};
let result = methods
.subscriptions_acknowledge(request, SUBSCRIPTION_NAME)
.doit();
match result {
Err(e) => {
println!("Ack error: {:?}", e);
}
Ok(_) => (),
}
}
// Wait for new messages. Print and ack any new messages.
fn subscribe_wait(methods: &PubsubMethods) {
check_or_create_subscription(&methods);
let request = pubsub::PullRequest {
return_immediately: Some(false),
max_messages: Some(1),
};
loop {
let result = methods
.subscriptions_pull(request.clone(), SUBSCRIPTION_NAME)
.doit();
match result {
Err(e) => {
println!("Pull error: {}", e);
}
Ok((_response, pullresponse)) => {
for msg in pullresponse.received_messages.unwrap_or(Vec::new()) {
let ack_id = msg.ack_id.unwrap_or(String::new());
let message = msg.message.unwrap_or(Default::default());
println!(
"message <{}> '{}' at {}",
message.message_id.unwrap_or(String::new()),
String::from_utf8(
base64::decode(&message.data.unwrap_or(String::new())).unwrap()
)
.unwrap(),
message.publish_time.unwrap_or(String::new())
);
if ack_id != "" {
ack_message(methods, ack_id);
}
}
}
}
}
}
// Publish some message every 2 seconds.
fn publish_stuff(methods: &PubsubMethods, message: &str) {
check_or_create_topic(&methods);
let message = pubsub::PubsubMessage {
// Base64 encoded!
data: Some(base64::encode(message.as_bytes())),
..Default::default()
};
let request = pubsub::PublishRequest {
messages: Some(vec![message]),
};
loop {
let result = methods.topics_publish(request.clone(), TOPIC_NAME).doit();
match result {
Err(e) => {
println!("Publish error: {}", e);
}
Ok((_response, pubresponse)) => {
for msg in pubresponse.message_ids.unwrap_or(Vec::new()) {
println!("Published message #{}", msg);
}
}
}
thread::sleep(time::Duration::new(2, 0));
}
}
// If called as '.../service_account pub', act as publisher; if called as '.../service_account
// sub', act as subscriber.
fn main() {
let client_secret =
oauth::service_account_key_from_file(&"pubsub-auth.json".to_string()).unwrap();
let client =
hyper::Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap()));
let mut access = oauth::ServiceAccountAccess::new(client_secret, client);
use yup_oauth2::GetToken;
println!(
"{:?}",
access
.token(vec![&"https://www.googleapis.com/auth/pubsub"])
.unwrap()
);
let client =
hyper::Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap()));
let hub = pubsub::Pubsub::new(client, access);
let methods = hub.projects();
let mode = env::args().nth(1).unwrap_or(String::new());
if mode == "pub" {
let message = env::args().nth(2).unwrap_or("Hello World!".to_string());
publish_stuff(&methods, &message);
} else if mode == "sub" {
subscribe_wait(&methods);
} else {
println!("Please use either of 'pub' or 'sub' as first argument to this binary!");
}
}