0
votes

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 [email protected]. This account has read access to the Google Ads accounts [email protected] and [email protected].

When I create two connections to Google Ads with the email [email protected] for the accounts [email protected] and [email protected], 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 [email protected] and to decline the access to my app manually and connect again with [email protected]. But then [email protected] 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));
1
what do you mean by become only one refresh token? Each user that authenticated your code will get a new access token and a refresh token. Please include your code. - DaImTo
Each user with a different account. I am connecting with one account for 2 google ads accounts. - Mutatos
Each user with a different account will return a different refresh token. Becouse you are using PHP that refresh token is only returned the first time you authenticate the user Google assumes that you have saved it. You should really consider using the google api php client library. - DaImTo
What is the difference if I use the google api php client lib? I am using now the googleads/googleads-php-lib. - Mutatos

1 Answers

0
votes

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.