calling google cloud functions from java spring boot application - java

I'm using google cloud functions with no problem in my mobile app using specific sdk for iOS and swift. For integrations reason I would now need to call one of my functions from a java springboot server.
I cannot find any example for that, does a specific client and example exist ?
I can find examples for other platform but not for java. Am I missing something ? can someone move me in the right direction ?
Thanks

You could simply do HTTP requests, that will trigger the cloud functions. Here is the documentation

GCP has documentation regarding the authentication on Cloud Functions. Since you need to authenticate on a Cloud Function from a VM inside your GCP project, I recommend you to read this section.
As explained, you should get an Identity Token that can authenticate on Cloud Functions. You can fetch the token either from gcloud or the metadata server. After you have that in whatever format you can access it without compromising your credentials (e.g. environment variable, file outside the repository) just perform the HTTP request to the trigger of the function.
In the end you should have something like this:
cf_trigger = "https://<region>-<project_id>.cloudfunctions.net/<function_name>"
identity_token = "foo"
HttpRequest.newBuilder()
.uri(URI.create(cf_trigger))
.header("Authorization", "bearer " + identity_token)
.build();

Related

Google Cloud Java project cannot find endpoint library

I am trying to connect an app-engine application to a Tensforflow model running on a Vertex AI endpoint.
It works if I provide my personal authentication details but these need to be refreshed all the time.
Going through the documentation it sounds like App Engine can authenticate to an endpoint automatically using the endpoint APIs - Based on this example
Unfortunately Eclipse cannot find "com.google.cloud.aiplatform.*" and I am not sure which library should include this.
The documentation provides a list of all the standard libraries which can be defined in the Eclispe built-path but I am under the impression the endpoint API is not part of it.
Would be great to understand if there is a standard library I could add to make this work like "Google Cloud Platform Libraries -> Cloud Datastore"
Alternatively I could use another authentication method like an API keys but I could not figure out which service account needs which API key to authenticate against my end point.
Thank you
I am trying to compile the sample example provided here with Java 8 but Eclipse cannot find the library.
I was expecting to activate a standard Google Cloud library to get the example running and then figure out the authentication against the Google Cloud Endpoint.

How can I get id_token of gcloud using java instead of gcloud command

Can someone give me a piece of java code to get id_token on GCP using google account?
I've got a restful api endpoint service deployed on GCP to test. I can use my google account to get the id_token using command "gcloud config config-helper"(e.g. id_token: eyJadhI6....). This id_token is then placed in the header as the value of authorization. The REST request is fired as expected. But I need to do the same using Java instead of from console command line to get a valid id_token.
I encourage you to use one of the Google-provided client libraries (SDKs).
These are open-sourced and tested by Google and provide you with a much better platform for your development.
See:
https://cloud.google.com/apis/docs/cloud-client-libraries
https://github.com/googleapis/google-cloud-java
and:
https://github.com/googleapis/google-cloud-java#authentication
Using these SDKs you will avoid the complexity in navigating your way through the underlying REST APIs directly.
Couple of useful tricks|pointers:
Google APIs Explorer enables you to use the REST APIs from the browser
If you append --log-http to any gcloud command, the REST calls are logged.
NB There are 2 flavors of APIs (API Client Libraries and Cloud Client Libraries) see explanation. The Cloud Client Libraries are more modern, are idiomatic and are preferred if you're mostly using GCP.
HTH!

How to generate oauth token, client id and secret using gcloud or Google Cloud Java SDK

I have a very big task, I need to automate the process of creating Dialogflow agents, for which I need to first create a Google project, which I have figured out using Java SDK, then I need to get client id and client secret to get an access token to send it to Dialogflow agent creation REST call, I just don't know how to generate a Oauth credentials for a project.
I'm using Java, Play framework. I even got success creating service account but just can't figured out how to generate Oauth creds.
Well, after doing some more research, turns out there is no API or there is no way to create oauth credentials clientId and client_sercret / client_secret.json file programmatically. One has to create it from google console itself.
There are several ways to call a Google API (Dialogflow) one is via a service account.
You mention you have already created a Service Account.
Next you can automate the credential creation via the Google API Client Library for Java as mention here.
Finally just call the Google API
You can find the full documentation of DialogFlow for Java here

Facebook Account-Kit + React.js + Spring boot

I'm starting a new project with Spring boot as backend and React.js as frontent.
Right now I'm struggling with Login/Authentication issue. I would like to provide Account-Kit from facebook but have few queries.
One of them is from where should I create a request to FB sdk for access token? Should it be server (spring), or react which then posts that data to server? Is it possible to call Accout-Kit from java?
I'm quite new to the topic and would be grateful for any answer.
You should call AccountKit from your client side js. For additional security, you can use code flow (instead of token flow) and pass the code to your server side. From there you can make a REST call to the Graph API and exchange that code + app secret for a token from server side. There is an example on the documentation that shows this flow using node.js, you should be able to make some server side changes to make it run on your setup.
Also, have you tried using react-account-kit-web?

Access GAE datastore with spray

I want to use GAE datastore to store my data but instead of the java API we want to use the JSON API and making requests through spray.
But before I can even do any request I need to obtain an access token.
I can't figure out how this is done with either the Java API or any other means. Is there a way to obtain an access token which can then be used for the JSON API (through spray)?
Like many Google services, the Datastore API uses OAuth for authentication. The easiest way to use it is with one of the Google API client libraries.
Java: https://developers.google.com/api-client-library/java/
Java + Datastore: https://developers.google.com/api-client-library/java/apis/datastore/v1beta2
The documentation for the client does a pretty good job of explaining how OAuth works and how to get started calling APIs by registering your app with the Console.
(I'm not familiar with spray, but I assume you'd be able to use the Java client from Scala.)

Categories