我在用PHP接收发送的MCC链接邀请时遇到了麻烦。
“我的客户中心”帐户不是顶级帐户,它是子“我的客户中心”。
我可以使用API正确发送邀请(从帐户9547216945发送到客户端1096054675),问题是当我使用Oauth2代表该客户端访问并尝试接受邀请时。
假设我已经毫无问题地发送了邀请。
我正在使用自己的Adwords帐户进行测试,所以这就是我的测试客户。
然后我按照以下步骤接受:
$clientId = "834249844................ak68sd6t.apps.googleusercontent.com";
$clientSecret = "uJdPPC..............CN";
$callbackUrl = "http://example.com/test.php";
$oauth2 = new OAuth2([
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'redirectUri' => $callbackUrl,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'scope' => 'https://www.googleapis.com/auth/adwords',
]);
if (!isset($_GET['code'])) {
$oauth2->setState(sha1(openssl_random_pseudo_bytes(1024)));
$_SESSION['oauth2state'] = $oauth2->getState();
// Redirect the user to the authorization URL.
$config = [ 'access_type' => 'offline', 'prompt' => 'consent'];
header('Location: ' . $oauth2->buildFullAuthorizationUri($config));
exit;
}
elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state.');
}
else {
$oauth2->setCode($_GET['code']);
$authToken = $oauth2->fetchAuthToken();
$oAuth2Credential = (new OAuth2TokenBuilder())
->withClientId($clientId)
->withClientSecret($clientSecret)
->withRefreshToken($authToken['refresh_token'])
->build()
;
// Construct an API session configured from the OAuth2 credentials above.
$session = (new AdWordsSessionBuilder())
->withDeveloperToken("m2l..........Vq3g")
->withOAuth2Credential($oAuth2Credential)
->withClientCustomerId("1096054675")
->build()
;
$adWordsServices = new AdWordsServices();
$linkOp = new LinkOperation();
$link = new ManagedCustomerLink();
$link->setClientCustomerId('1096054675');
$link->setManagerCustomerId('9547216945');
$link->setLinkStatus(LinkStatus::ACTIVE);
$linkOp->setOperand($link);
$linkOp->setOperator(Operator::ADD);
$managedCustomerService = $adWordsServices->get($session, ManagedCustomerService::class);
$result = $managedCustomerService->mutateLink([$linkOp]);
}
该脚本使我登录到客户端帐户(我用于测试目的的帐户),然后我将该客户端刷新令牌与Oauth2一起使用,并且当我要接受先前发送的邀请时,出现此错误:
[ManagedCustomerServiceError.NOT_AUTHORIZED @操作[0]]
我试图使其多次运行都没有成功,有人知道我在这里做错了吗?
谢谢!
问题是我必须使用
$linkOp->setOperator(Operator::SET)
代替
$linkOp->setOperator(Operator::ADD)
为了接受现有的待处理邀请。