Error accepting MCC invitation with PHP

52 views
Skip to first unread message

Facundo Fasciolo

unread,
Nov 6, 2018, 6:15:51 AM11/6/18
to AdWords API and Google Ads API Forum
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
#################################################################################
$clientId     = "834249844................ak68sd6t.apps.googleusercontent.com";
$clientSecret = "uJdPPC..............CN";
$callbackUrl  = "http://mysite.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]);
}
#################################################################################


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!

Facundo Fasciolo

unread,
Nov 6, 2018, 6:20:30 AM11/6/18
to AdWords API and Google Ads API Forum
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
#################################################################################
$clientId     = "834249844................ak68sd6t.apps.googleusercontent.com";
$clientSecret = "uJdPPC..............CN";
$callbackUrl  = "http://mysite.com/test.php";
 

$oauth2 = new OAuth2([
    'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/taoken',

Facundo Fasciolo

unread,
Nov 6, 2018, 6:20:31 AM11/6/18
to AdWords API and Google Ads API Forum
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
#################################################################################
$clientId     = "834249844................ak68sd6t.apps.googleusercontent.com";
$clientSecret = "uJdPPC..............CN";
$callbackUrl  = "http://mysite.com/test.php";
 

$oauth2 = new OAuth2([
    'authorizationUri'      => 'https://accounts.google.com/o/oauth2/v2/auth',
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',

Teja Makani

unread,
Nov 6, 2018, 12:38:34 PM11/6/18
to AdWords API and Google Ads API Forum
Hello Facundo,

The error ManagedCustomerServiceError.NOT_AUTHORIZED indicates that the user making the API call is not authorized to make API call. Looks like you are trying to accept the invitation sent by the MCC account. You need to make sure the user authenticating the API call must have administrative access to the account. You could refer this article to grant account access to the user, before giving access please refer the access levels guide from here.

Please give it a try and let me know if you are able to accept the invitation. If you are facing any issues please share the entire SOAP logs(request and response), you could refer this guide to enable logging and can use reply privately to the author option while sharing the details requested.

Regards,
Sai Teja, AdWords API Team.

Facundo Fasciolo

unread,
Nov 7, 2018, 12:37:47 PM11/7/18
to AdWords API and Google Ads API Forum
Hello Teja!

I could solve this issue, I'll share the problem in case it can help another person.

The problem was that I had an error in code:

When trying to accept the invitation, instead of doing:
 $linkOp->setOperator(Operator::ADD);

I had to do:
 $linkOp->setOperator(Operator::SET);


That was my error.

Thank you!

Teja Makani

unread,
Nov 7, 2018, 2:55:18 PM11/7/18
to AdWords API and Google Ads API Forum
Hello Facundo,

Glad the problem is resolved. Thanks for sharing your solution, it might be helpful for others.


Regards,
Sai Teja, AdWords API Team.

On Tuesday, November 6, 2018 at 6:15:51 AM UTC-5, Facundo Fasciolo wrote:
Reply all
Reply to author
Forward
0 new messages