Skip to content

Instantly share code, notes, and snippets.

@oxydron
Created December 10, 2019 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oxydron/4343919616316c0a039797fa40873aa5 to your computer and use it in GitHub Desktop.
Save oxydron/4343919616316c0a039797fa40873aa5 to your computer and use it in GitHub Desktop.
Google Cloud Dart API pubsub example code. I'm sharing this code to help those that will try to use this lib on dart and maybe will find the docs poor to achieve this example.
import 'package:googleapis/pubsub/v1.dart';
import 'package:googleapis_auth/auth_io.dart';
import 'credentials.dart';
const _SCOPES = const [PubsubApi.PubsubScope];
Future<PublishResponse> pubsubExample(Map<String, String> data) =>
clientViaServiceAccount(PUBLISH_PUBSUB_CREDENTIALS, _SCOPES).then((http) {
PubsubMessage msg = PubsubMessage();
msg.attributes = data;
PublishRequest publishRequest = PublishRequest();
publishRequest.messages = [msg];
String topic = 'projects/<YOUR PROJECT>/topics/<YOUR_TOPIC>';
PubsubApi pubsubApi = PubsubApi(http);
return pubsubApi.projects.topics
.publish(publishRequest, topic)
.then((publishResponse) => publishResponse);
});
"credentials.dart':
import 'package:googleapis_auth/auth_io.dart';
final PUBLISH_PUBSUB_CREDENTIALS = ServiceAccountCredentials.fromJson({
"type": "service_account",
"project_id": "YOUR PROJECT ID",
"private_key_id": "YOUR PRIVATE KEY ID",
"private_key":"YOUR PRIVATE KEY"
"client_email": "YOUR EMAIL",
"client_id": "YOUR ID",
"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/user-publish%40ligre-dev.iam.gserviceaccount.com"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment