简体   繁体   English

Google API Oauth2:连接帐户只有一个刷新令牌

[英]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.当我的@gmail.com 帐户对两个不同的 Google Ads 帐户具有读取权限时,我正在使用 Google Oauth2 API 连接到 Google Ads 并仅成为一个刷新令牌。

I have the account a@gmail.com.我有帐户 a@gmail.com。 This account has read access to the Google Ads accounts b@gmail.com and c@gmnail.com.此帐户对 Google Ads 帐户 b@gmail.com 和 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. 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联系。 Why?为什么? How can I manage this, to become 2 refresh tokens for each connection?我该如何管理这个,成为每个连接的 2 个刷新令牌? 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.成为第二次连接的刷新令牌的唯一方法是将 go 转到我的帐户 a@gmail.com 并手动拒绝访问我的应用程序并再次使用 c@gmail.Z4D236D9A2D102C5FE6AD1C50DA4B But then b@gmail.com has no refresh token anymore.但是 b@gmail.com 不再有刷新令牌。

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.通过阅读来自 Google 的文档和许多论坛帖子,我发现的唯一解决方案是当有人想要通过一个经理帐户与两个或多个不同的 Google Ads 帐户连接时复制 refresh_token。

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.我的解决方案: - 通过将经理帐户的 email 保存为单独的值来连接到第一个帐户 - 将此条目作为活动连接保存在数据库中 - 通过再次保存经理帐户的 email 来连接到第二个帐户。 In this case, Google doesn't deliver a refresh_token, because the app is already connected by the first connection.在这种情况下,Google 不会提供 refresh_token,因为应用程序已经通过第一个连接进行连接。 That's why I am getting the refresh_token from the entry of the first connection.这就是为什么我从第一个连接的条目中获取 refresh_token 的原因。 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.因为您可以断开所有连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM