Easy, it is not – when authentication is required for the function

You need to update the Server client id the string.xml file:

Advertisements
<string name="server_client_id">YOUR_SERVER_CLIENT</string>

Then you need to add credentials for your app in the Google Cloud console:

Enable Google Sign in

<application>
….
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
view raw AndroidManifest.xml hosted with ❤ by GitHub
implementation 'com.google.android.gms:play-services-auth:19.0.0'
dependencies {
classpath 'com.google.gms:google-services:4.3.4'
}
private GoogleSignInClient mGoogleSignInClient;
view raw GSI1.java hosted with ❤ by GitHub
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.server_client_id))
.requestEmail()
.build();
mGoogleSignInClient = getClient(this, gso);
view raw GSI2.java hosted with ❤ by GitHub
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
view raw GSI3.java hosted with ❤ by GitHub
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
updateUI(account);
} catch (ApiException e) {
e.printStackTrace();
updateUI(null);
}
}
@Override
public void onStart() {
super.onStart();
GoogleSignInAccount account = getLastSignedInAccount(this);
updateUI(account);
}
view raw GSI4.java hosted with ❤ by GitHub
private void updateUI(@Nullable GoogleSignInAccount account) {
if (account != null) {
String idToken = account.getIdToken();
// call function with the token
callGcpFunction(idToken);
} else {
signIn();
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
view raw GSI5.java hosted with ❤ by GitHub
Advertisements

On Google side, configure the permissions of the function to have the invoker role.

And finally, call the HTTP url of the cloud function with the id token:

URL url = new URL("https://<place the function url here>");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Bearer " + idtoken);
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);

// Read the output:
try (BufferedReader br = new BufferedReader(
        new InputStreamReader(con.getInputStream(), "utf-8"))) {
    StringBuilder response = new StringBuilder();
    String responseLine = null;
    while ((responseLine = br.readLine()) != null) {
        response.append(responseLine.trim());
    }
    System.out.println(response.toString());
}
Advertisements