2

I'm designing a voice application in Android using API.AI integrating Actions on Google (AoG) that triggers a Firebase Cloud Function.

You can see on the following image that my Android app implements a Google sign in process and also Firebase has Google user authentication.

I want to read my user information (email, name, etc.) on the Firebase Cloud Function but I couldn't do it.

I already done it using Google Home instead of an Android app.

Do you know any method to do this? I've read a lot of tutorials but no one what useful to me.


The most important part of the implementation on Android is the AIConfiguration:

final AIConfiguration config = new AIConfiguration(
                CLIENT_ACCESS_TOKEN,
                AIConfiguration.SupportedLanguages.English,
                AIConfiguration.RecognitionEngine.System);

Where CLIENT_ACCESS_TOKEN is obtained from API.AI (like here).

Also, the API.AI console page uses Webhook with the Firebase Cloud Function that triggers correctly.

The login method is implemented using FirebaseUI.

The most important parts in this configuration are Firebase and Actions on Google:

Firebase

You need to fill two sign-in options for Google:

1) Whitelist client IDs which are automatically added if you use SHA1 fingerprint in your Android project. By the way, my app was automatically added.

2) Web SDK configuration: Fields filled with the auto generated OAuth 2.0 client ID from Google Cloud Platform's Credentials console of my project.

Actions on Google

Here I set Account Linking like in this answer.


Thanks for your help.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Your diagram is a little confusing still. Are you calling API.AI's API from your app, or are you using the Google Assistant to call API.AI? Can you update the question to clarify this and show the code you're using for each of those steps and the code you're using on the cloud functions side that you think should be getting the user information? – Prisoner Sep 18 '17 at 16:52
  • Thanks for answering @Prisoner. I added more details. I thing the problem is more related to Account Linking or OAuth Client configuration other than Android. – Emiliano Borghi Sep 18 '17 at 17:33

2 Answers2

1

In a way, yes, the problem is with account linking.

The underlying problem, however, is that you're using the actions-on-google library, but calling API.AI the way you are isn't going through Actions on Google. So there are no accounts to link - you have the account you have from your Android app, and it is the same account that Firebase has, but you're not sending that to your fulfillment webhook.

API.AI, itself, doesn't have a concept of user authentication or user identity. (It does have authentication - but this is to make sure that the webhook is getting a legitimate request from API.AI. It has nothing to do with the user.) It relies on the developer to add this information elsehow. The Google Assistant has a few approaches to this depending on your needs, but if you're not building on the Google Assistant platform (and it sounds like you're not) and just going through API.AI directly, you'll need to include this information yourself.

I'm not too familiar with how to use the apiai-java-client, but it looks like part of each exchange you have with the server can include an RequestExtras object. As part of this object, you can include a context called (for example) "authenticated" that has parameters and one of these parameters can include authentication information (such as an auth token, although this may not be a great security practice).

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Which "this" do you mean? Using an AIContext? – Prisoner Sep 18 '17 at 20:09
  • Sorry for the misunderstood. I have no very clear about passing the authentication information using contexts. Is this available in API.AI? – Emiliano Borghi Sep 19 '17 at 12:21
  • I realized I made a mistake in the class name - I've corrected it and provided some additional links. I've also added a paragraph about how API.AI can handle authentication. I'm still not sure what you mean by "this" in your question, but hopefully I've answered you a little better. – Prisoner Sep 19 '17 at 12:53
0

Well the firebase functions can be triggered on Firebase activities like write to database, storage and a lot more. But as your picture suggests, there is an API.AI in between and you want cloud function to be triggered after using api.ai. Thus you can use HTTP triggers and send a request. Check out the official documentation and work it out as you need.

https://firebase.google.com/docs/functions/http-events

Here is a github sample doing so: https://github.com/firebase/functions-samples/tree/master/quickstarts/time-server

Apart from this, you can also use an Auth trigger but you have API.AI in between. So i assume that you don't want to do that. Either way, if you want, check this out at well.

https://firebase.google.com/docs/functions/auth-events

Hope it helps

Aman
  • 149
  • 13
  • I'm triggering correctly the Firebase Cloud Function. My problem arises when I try to read which Firebase user (email and/or email) triggered the HTTP event (using Android). – Emiliano Borghi Sep 18 '17 at 17:41