Google API Oauth2: Only one refresh token for connected accounts
I am using the Google Oauth2 API to connect to Google Ads and become only one refresh token when my @gmail.com account has read access to two different Google Ads accounts.
I have the account a@gmail.com. This account has read access to the Google Ads accounts b@gmail.com and c@gmnail.com.
When I create two connections to Google Ads with the email a@gmail.com for the accounts b@gmail.com and c@gmail.com, then I receive only one refresh token, for the first connection and none refresh token for the second connection. Why? How can I manage this, to become 2 refresh tokens for each connection? The only way to become a refresh token for the second connection is to go to my account a@gmail.com and to decline the access to my app manually and connect again with c@gmail.com. But then b@gmail.com has no refresh token anymore.
code:
$oauth2 = new OAuth2( [ 'authorizationUri' => $this->container->getParameter('oauth2.google.adwords.authorizationUri'), 'tokenCredentialUri' => $this->container->getParameter('oauth2.google.adwords.tokenCredentialUri'), 'redirectUri' => $this->container->getParameter('domain.system') . $this->container->getParameter( 'oauth2.google.adwords.redirectUri.advertiser' ), 'clientId' => $this->container->getParameter('oauth2.google.adwords.clientId'), 'clientSecret' => $this->container->getParameter('oauth2.google.adwords.clientSecret'), 'scope' => $this->container->getParameter('oauth2.google.adwords.scope') ] ); // Create a 'state' token to prevent request forgery. // Store it in the session for later validation. $randomState = sha1(openssl_random_pseudo_bytes(1024)) . '---' . $accountId; $oauth2->setState($randomState); // Redirect the user to the authorization URL. $config = [ // Set to 'offline' if you require offline access. 'access_type' => 'offline', 'approval_prompt' => 'force' ]; // redirect to google ads return new RedirectResponse($oauth2->buildFullAuthorizationUri($config));
The only solution that I found by reading the documentation from Google and many forum posts is to copy the refresh_token when someone wants to connect over one manager account with two or more different Google Ads accounts.
My solution: - connect to the first account by saving the email of the manager account as a separate value - save this entry in the DB as an active connection - connect to the second account by saving again the email of the manager account. In this case, Google doesn't deliver a refresh_token, because the app is already connected by the first connection. That's why I am getting the refresh_token from the entry of the first connection. It works fine for me!
If you make this solution, don't forgot to check for available connections from this manager account, when you disconnect. Because you can disconnect all the connections.