Actions on Google account linking

后端 未结 2 1764
孤街浪徒
孤街浪徒 2021-01-21 11:37

Following documentation described here, I have account linking set up with implicit grants and find that it works well when testing with the browser / actions console, and also

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 12:29

    With help from Google support & engineering, this is now resolved:

    1. As noted above, I had to include this meta tag:
    2. I needed to have https://my-auth-endpoint.com/ in my project's authorized redirect URI. It is not enough to have it only in Authorized javascript origins. The other key thing is to include the trailing slash, I hadn't originally and it will not work without it.

    Below is the simple code foundation you can use to get a working version of an authorization endpoint for actions on google account linking:

    .html:

    
    
    
      Authorization Page
      
      
      
      
      
         
      
        
      
    
    
    
    Google Sign In
    Sign in with your Google account



    .js:

    // Retrieve user data, store to DynamoDB and complete the redirect process to finish account linking
    function onSignIn(googleUser) {
      let profile = googleUser.getBasicProfile(),
          id = profile.getId(),
          name = profile.getName(),
          email = profile.getEmail(),
          token = googleUser.getAuthResponse().id_token,
          redirect_uri = 'https://oauth-redirect.googleusercontent.com/r/vital-code-16xxxx',
          jsonData = JSON.parse(sessionStorage['jsonData']),
          redirectURL = redirect_uri + '#access_token=' + token + '&token_type=bearer&state=' + jsonData.state
    
      // store the user's name, ID and access token
      storeUserData(email, name, id, token, function() {
        // sign out of google for this app
        let auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut()
        // if this page was loaded by Actions On Google, redirect to complete authorization flow
        typeof redirect_uri != 'undefined' ? window.location = redirectURL : void 0    
      })   
    }
    
    // Store the user data to db
    function storeUserData (email, name, id, token, callback) {
      // removed for simplicity
    }
    
    // Store URI query variable 'state' to browser cache
    function storeQueryVariables() {
      let qvar = {
        'state': getQueryVariable('state')
      }
      storeLocally(qvar)
    }
    
    // Get any variable from incoming URI
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split('&');
      for (var i = 0; i < vars.length; i++) {
          var pair = vars[i].split('=');
          if (decodeURIComponent(pair[0]) == variable) {
              return decodeURIComponent(pair[1]);
          }
      }
      console.log('Query variable %s not found', variable);
    }
    
    // Store JSON object input to local browser cache 
    function storeLocally (jsonData) {
      if (typeof(Storage) !== 'undefined') {
        sessionStorage['jsonData'] = JSON.stringify(jsonData)
      } else {
        console.log('Problem: local web storage not available')
      }
    }
    

提交回复
热议问题