DialogFlow/Actions: Allow Google Assistant user to create Event in Google Calendar from Actions App

前端 未结 1 1800
死守一世寂寞
死守一世寂寞 2021-01-27 07:56

Target/Summary: I have an Actions App developed in Google DialogFlow and I want the user be able to create Google Calendar Event using the App (from Google Assi

相关标签:
1条回答
  • 2021-01-27 08:20

    Here are the "missing" steps after which I got this thing working. I am sure there must be alternate ways of doing this but I will suffice with this until I get some feedback here.

    Note: Google Actions Console UI has changed so the screens in the original question maybe different (but are complete).

    1. Make sure the Calendar API is enabled in the Google Cloud Console (API Library) against the project you have selected.

    1. Go back to https://manage.auth0.com > APIs. Edit the listed System API > Machine to Machine Applications. Authorize your listed Application and select/tick the following two scopes:

      read:users
      read:user_idp_tokens

    1. Go to Google Actions Console > Account Linking (see point no. 4) and check if you have set the following scopes:

    1. Change your code by:
      a. Call https://[domain].auth0.com/userinfo with the Authorization token from originalRequest.data.user.accessToken (see point 7 and 8). Successful response should give you the userId
      b. Post on https://[domain].auth0.com/oauth/token with request body containing your client_id, client_secret, audience, grant_type. Successful response should give you a new access_token.
      c. Change the Authorization token to the newly acquired access_token from point 11.b and make a call to https://[domain].auth0.com/api/v2/users/[userId] where the userId is the one you have from point 11.a. Successful response should give you a "Google" access_token and userId (under identities).
      d. Change the authorization token in the header with the one from point 11.c. This is the token you use to call the Google APIs. For example, for Calendars, call https://www.googleapis.com/calendar/v3/users/me/calendarList. You will get the required response.

    Here is the code (I have reused the variables):

    string responseText = string.Empty;
    string clientId = "DCuPWHknmv_k2xxxxxxxxxxxxxxxxx";     //Auth0 ClientId
    string clientSecret = "7G3xlreFIULPZ9OtwlOxCX99xxxxxxxxxxxxxxxxxxx";    //Auth0 ClientSecret
    string accessToken = jsonObject.SelectToken("originalRequest.data.user.accessToken").ToString();
    
    try
    {
        using (var httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
            var url = "https://xxx.auth0.com/userinfo";
            responseText = httpClient.GetStringAsync(url).Result;
    
            JObject jsonUserInfo = JObject.Parse(responseText);
            string userId = jsonUserInfo.SelectToken("sub").ToString();
    
            url = "https://xxx.auth0.com/oauth/token";
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("client_id", clientId),
                new KeyValuePair<string, string>("client_secret", clientSecret),
                new KeyValuePair<string, string>("audience", "https://[domain].auth0.com/api/v2/"),
                new KeyValuePair<string, string>("grant_type", "client_credentials")
            });
    
            var postResult = httpClient.PostAsync(url, content).Result;
            jsonUserInfo = JObject.Parse(postResult.Content.ReadAsStringAsync().Result);
            accessToken = jsonUserInfo.SelectToken("access_token").ToString();
    
            httpClient.DefaultRequestHeaders.Remove("Authorization");
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
    
            url = "https://xxx.auth0.com/api/v2/users/" + userId;
            jsonUserInfo = JObject.Parse(httpClient.GetStringAsync(url).Result);
            accessToken = jsonUserInfo.SelectToken("identities[0].access_token").ToString();
            userId = jsonUserInfo.SelectToken("identities[0].user_id").ToString();
    
            httpClient.DefaultRequestHeaders.Remove("Authorization");
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
    
            url = "https://www.googleapis.com/calendar/v3/users/me/calendarList";
            responseText = httpClient.GetStringAsync(url).Result;
        }
    }
    
    0 讨论(0)
提交回复
热议问题