Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get email from user using Google actions

I want to recieve the user email using Google Actions as documented here but docs talks about EMAIL permission, but when I read the permission docs here I can't find any EMAIL permission. Any help? How can I receive the user email?

like image 403
greywolf82 Avatar asked May 20 '17 13:05

greywolf82


2 Answers

This is WORKING ,you can do this with account linking.

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

    • goto API's and 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
    • goto Authorised Redirect URIs
    • White list our app's URL, in this URL fixed part is https://oauth-redirect.googleusercontent.com/r/[project-Id] (replace [project-Id] with your project id)
    • Save the changes
  2. Go to Actions on Google(https://console.actions.google.com) -> Account linking setup

    • select Grant type = Authorisation code
    • Client info

      • Fill up client id,client secrtet, auth_uri, token_uri
      • Enter any random url as Authorization URL and token_uri such as https://example.com/auth and https://example.com/token
      • Save
      • It will show an error while running on the google assistant, but dont worry
      • Come back to the account linking section in the assistant settings and this time enter correct auth_uri as https://accounts.google.com/o/oauth2/auth and token_uri as https://accounts.google.com/o/oauth2/token

        note that it is some sort of problem in from their side that not allows you to use this url in first hit and will keep saying "Generic URLs are not allowed. You must provide a valid token url specific to your Assistant app." so just give any random url in first hit and save, then comeback again it will allow you these urls :-)

      • Put the scopes as https://www.googleapis.com/auth/userinfo.profile and https://www.googleapis.com/auth/userinfo.email and weare good to go.

      • 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. write this code

    accessToken = req.get("originalRequest")
                     .get("data")
                     .get("user")
                     .get("accessToken")
    
    r = requests.get(link) // make get request to link
    
    print("Email Id: " + r.json()["email"])
    print("Name: " + r.json()["name"])
    
like image 86
Jatin Mahajan Avatar answered Oct 09 '22 03:10

Jatin Mahajan


Ya, unfortunately the Assistant's SDK doesn't give you the email address. But if you implement account linking (like Ahmed mentioned) and use the Streamlined Flows, then you'll be getting the email provided to you; you just need to use the jsonwebtoken library and you can decode the assertion JWT and grab the email address.

That being said, this happens during "sign in" and token exchange... not during the actual action fulfillment. You'll need to issue a refresh token / access token :S

like image 27
kismet31 Avatar answered Oct 09 '22 04:10

kismet31