Hello.
I'm having some trouble accepting a sent MCC link invitation with PHP.
The MCC account is not the top-level account, it is a sub MCC.
I can send the invitation correctly (from account
9547216945 to client 1096054675) usign the API, the problem is when I use Oauth2 to access on behalf of that client and try to accept the invitation.
Supose I have sent the invitation with no problem.
I'm doing test using an Adwords account of my own, so that is my test client.
Then I follow next steps to accept:
#################################################################################
#File: test.php
#################################################################################
$clientSecret = "uJdPPC..............CN";
$oauth2 = new OAuth2([
'redirectUri' => $callbackUrl,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
]);
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->setLinkStatus(LinkStatus::ACTIVE);
$linkOp->setOperand($link);
$linkOp->setOperator(Operator::ADD);
$managedCustomerService = $adWordsServices->get($session, ManagedCustomerService::class);
$result = $managedCustomerService->mutateLink([$linkOp]);
}
#################################################################################
That script makes me login into the client account (the one that I'm using for testing purpose), then I use that client refresh token with Oauth2, and when I want to accept the previous sent invitation, I get this error:
[ManagedCustomerServiceError.NOT_AUTHORIZED @ operations[0]]
I tried to make it work multiple times with no success, anyone know what I'm doing wrong here?
Thank you!