Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate user with just a Google account on Actions on Google?

Currently Google Assitant includes an easy way to request non-identifiable information about the user and a detailed flow to authenticate the user on a third party service through OAuth2. What about if all I need is have the user authenticate on Google itself? I tried filling in the account linking flow using Google OAuth2 information, but that seems not to work. If that last thing is supposed to work fluently than that would be enough of an answer.

Context: Users already authenticate only with Google on a related webpage. All I need is to link this already authenticated account with the less-authenticated account on Google Assistant.

like image 948
David Mulder Avatar asked May 31 '17 15:05

David Mulder


3 Answers

Update, 25 Oct 2018:

As of 13 September 2018, there is now a much simpler way to access the user's account if your project uses Google Sign-In. Google Sign-In for Assistant will give you an ID Token with information about the user, including their Google ID, with their permission. This permission can be granted just using voice and is fairly streamlined.

You can combine this with a web- or app-based Google Sign-In to get their permission to access OAuth scopes if you need to access Google's APIs.

Update, 25 Oct 2017:

As of around 4 Oct or 7 Oct, Google has updated their policy (again) to restore language restricting OAuth endpoints that are valid. The terms now include

When implementing account linking using OAuth, you must own your OAuth endpoint

and it appears (from the comments below) that they now check for the Google endpoints to prevent this method from working.

At this point, the only thing you can do is setup your own OAuth2 server.

Original Post:

Broadly speaking, the auth tasks you need to do are in four parts:

  1. Configure your project (in the cloud console) so that the Calendar API is enabled and that the OAuth2 client is correctly configured.
  2. Configure the Action for account linking in the action console.
  3. Configure the Actions on Google Integration for your API.AI Agent to indicate that sign-in is required.
  4. When API.AI calls your webhook to fulfill an Intent, it will include an auth token as part of the JSON. You can use this token to make calls to the Google APIs you need.

Configure Cloud Project

You need to configure your cloud project so that it has access to the Google APIs you need and setup the OAuth2 Client ID, Secret, and Redirect URI.

  1. Go to https://console.cloud.google.com/apis/dashboard and make sure you have the project you're working with selected. Then make sure you have the APIs you need enabled.

  2. Select the "Credentials" menu on the left. You should see something like this:

Credentials screen

  1. Select "Create credentials" and then "OAuth client ID" Create credentials

  2. Select that this is for a "Web application" (it is... kinda...)

  3. Enter a name. In the screen shot below, I used "Action client" so I remember that this is actually for Actions on Google.

  4. In the "Authorized Redirect URIs" section, you need to include a URI of the form https://oauth-redirect.googleusercontent.com/r/your-project-id replacing the "your-project-id" part with... your project ID in the Cloud Console. At this point, the screen should look something like this:enter image description here

  5. Click the "Create" button and you'll get a screen with your Client ID and Secret. You can get a copy of these now, but you can also get them later. enter image description here

  6. Click on "Ok" and you'll be taken back to the "Credentials" screen with the new Client ID added. You can click the pencil icon if you ever need to get the ID and Secret again (or reset the secret if it has been compromised).

enter image description here

Configure the Action Console

Once we have OAuth setup for the project, we need to tell Actions that this is what we'll be using to authenticate and authorize the user.

  1. Go to https://console.actions.google.com/ and select the project you'll be working with.

  2. In the Overview, make your way through any configuration necessary until you can get to Step 4, "Account Linking". This may require you to set names and icons - you can go back later if needed to correct these.

enter image description here

  1. Select the Grant Type of "Authorization Code" and click Next.

enter image description here

  1. In the Client Information section, enter the Client ID and Client Secret from when you created the credentials in the Cloud Console. (If you forget, go to the Cloud Console API Credentials section and click on the pencil.)

  2. For the Authorization URL, enter https://accounts.google.com/o/oauth2/v2/auth

  3. For the Token URL, enter https://www.googleapis.com/oauth2/v4/token

  4. Click Next

enter image description here

  1. You now configure your client for the scopes that you're requesting. Unlike most other places you enter scopes - you need to have one per line. Then click Next.

enter image description here

  1. You need to enter testing instructions. Before you submit your Action, these instructions should contain a test account and password that the review team can use to evaluate it. But you can just put something there while you're testing and then hit the Save button.

Configure API.AI

Over in API.AI, you need to indicate that the user needs to sign-in to use the Action.

  1. Go to https://console.api.ai/ and select the project you're working with.

  2. Select "Integrations" and then "Actions on Google". Turn it on if you haven't already.

  3. Click the "Sign in required for welcome intent" checkbox.

enter image description here

Handle things in your webhook

After all that setup, handling things in your webhook is fairly straightforward! You can get an OAuth Access Token in one of two ways:

  • If you're using the JavaScript library, calling app.getUser().authToken

  • If you're looking at the JSON body, it is in originalRequest.data.user.accessToken

You'll use this Access Token to make calls against Google's API endpoints using methods defined elsewhere.

You don't need a Refresh Token - the Assistant should hand you a valid Access Token unless the user has revoked access.

like image 72
Prisoner Avatar answered Nov 16 '22 08:11

Prisoner


After contacting Google the current situation seems to be that you should set up your own OAuth2 server, and then on the login screen of your OAuth2 server you should start the Google OAuth2 flow.

you have to have your own endpoint with Google Oauth2 - it is correct that you can't use Google Oauth itself as a provider. To use the Google OAuth service, you can use a "sign in with Google" button in your own endpoint instead.

Source: Contacting Google Actions on Google Support

Kind of speechless right now... as this seems to be a huge oversight on Google's part.

like image 43
David Mulder Avatar answered Nov 16 '22 06:11

David Mulder


I am able to make it work after a long time. We have to enable the webhook first and we can see how to enable the webhook in the dialog flow fulfillment docs If we are going to use Google Assistant, then we have to enable the Google Assistant Integration in the integrations first. Then follow the steps mentioned below for the Account Linking in actions on google:-

  1. Go to google cloud console -> APIsand Services -> Credentials -> OAuth 2.0 client IDs -> Web client -> Note the client ID, client secret from there -> Download JSON - from json note down the project id, auth_uri, token_uri -> Authorised Redirect URIs -> White list our app's URL -> in this URL fixed part is https://oauth-redirect.googleusercontent.com/r/ and append the project id in the URL -> Save the changes

  2. Actions on Google -> Account linking setup 1. Grant type = Authorisation code 2. Client info 1. Fill up client id,client secrtet, auth_uri, token_uri 2. Enter the auth uri as https://www.googleapis.com/auth and token_uri as https://www.googleapis.com/token 3. Save and run 4. It will show an error while running on the google assistant, but dont worry 5. Come back to the account linking section in the assistant settings and enter auth_uri as https://accounts.google.com/o/oauth2/auth and token_uri as https://accounts.google.com/o/oauth2/token 6. Put the scopes as https://www.googleapis.com/auth/userinfo.profile and https://www.googleapis.com/auth/userinfo.email and weare good to go. 7. Save the changes.

  3. In the hosting server logs, we can see the access token value and through access token, we can get the details regarding the email address.

  4. Append the access token to this link "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" and we can get the required details in the resulting json page.
  5. accessToken = req.get("originalRequest").get("data").get("user").get("accessToken")
    r = requests.get(link) print("Email Id= " + r.json()["email"]) print("Name= " + r.json()["name"])
like image 2
Jatin Mahajan Avatar answered Nov 16 '22 08:11

Jatin Mahajan