title: Google Actions Account Linking Flow
body: <p>I've written an authorization flow for my action, to do account linking with the "Implicit flow". I have been testing it using the web simulator. When I try to run the action in the web simulator it tells me to link my account, and provides a URL:</p>
  
  <p>"<a href="https://assistant.google.com/services/auth/handoffs/auth/start?provider=hello_dev&amp;return_url=https://www.google.com/" rel="nofollow noreferrer">https://assistant.google.com/services/auth/handoffs/auth/start?provider=hello_dev&amp;return_url=https://www.google.com/</a>"</p>
  
  <p>I run that URL and this is what happens:</p>
  
  <ul>
  <li>My Java servlet receives the auth request from google. It contains what is documented, a GOOGLE_CLIENT_ID, a REDIRECT_URI, a STATE and "response_type=token"</li>
  <li>I create an access-token from the Google ID that is logged in by using UserServiceFactory.getUserService().getCurrentUser().getUserId()</li>
  <li>I craft a URL to the redirect-uri that contains my ACCESS_TOKEN, token_type=bearer, and state= the state string I originally received</li>
  <li>I create a web page for the user that contains that link so they can click it and it executes the link and my account is linked. </li>
  </ul>
  
  <p>That works great and the response is a blank page with a URL that says: "<a href="https://www.google.com/?result_code=SUCCESS&amp;result_message=Accounts+now+linked" rel="nofollow noreferrer">https://www.google.com/?result_code=SUCCESS&amp;result_message=Accounts+now+linked</a>." </p>
  
  <p>However, I wanted a flow that allowed the user to ensure they are logged in, and logged into the Google Account they wanted to link</p>
  
  <p>So, I also present a link that logs them out if they'd like. The link is created by using userService.createLogoutURL(thisUrl)</p>
  
  <p>If they do that, I then present them a link to log in, created using userService.createLoginURL(thisUrl)</p>
  
  <p>Once they have logged in, with a different Google ID,  they are back on the original page which allows them to log out again or click the "link account" link.  </p>
  
  <p>The situation is, that if they click the "link account" link now, the EXACT same link that would have worked successfully before they logged out (except a different ACCESS_TOKEN value) and logged back in with a different account, the response is a blank page with a URL that says: "<a href="https://www.google.com/?result_code=FAILURE&amp;result_message=Account+linking+failed" rel="nofollow noreferrer">https://www.google.com/?result_code=FAILURE&amp;result_message=Account+linking+failed</a>" </p>
  
  <p>Two things of note: </p>
  
  <ul>
  <li>If they log out and log back in with the same account, then the "link account" URL works fine</li>
  <li>If they are not logged in at all when beginning the process, and then log in and then use the "link account" link it works fine</li>
  </ul>
  
  <p>So, the only time it fails is when they are logged in, log out, log in with a different account, and click to link accounts. That's when it fails.</p>
  
  <p>The account doesn't matter, multiple different accounts work, as long as the browser is logged into the first account and doesn't change during the flow.  </p>
  
  <p>I honestly cannot imagine how this is happening.</p>
  
  <p>This gets even worse if I do it in the Google Home app, as it then returns some "malformed request error" page and basically stops working, PERMANENTLY. Stopping/Starting the action didn't help. Stopping/Starting the Web service didn't help. Rebooting the Android device didn't help. Link Account from the Google Home app was permanently broken. The only way I was able to get it working again was to use the Account Link URL (gotten from the web simulator) in the Chrome Browser on my Android device, which seemed to "reset" everything. </p>