Adwords API grant access

527 views
Skip to first unread message

etienne....@admobio.com

unread,
Oct 9, 2015, 8:31:09 AM10/9/15
to AdWords API Forum
Hi everybody !

I'm new to Adwords API and on this forum. Feel free to comment if necessary!

I am currently developing a small web application that allows a visitor to give me access to its Adwords account then I am able to retrieve data from its campaigns.

I also need to detect if the customer has several accounts in order to make him choose from a list.

I read and tried a lot of documentation and I understand the principle of authentication.

I'm currently stuck and I can not find answer to my problems.

Here's the example of the current code which I work. This allows a user to authorize and then return it to my page and then I am able to retrieve an access token and a refresh token.

My problem is that I'm not able to retrieve information on the visitor and I dont know how to launch a first query to retrieve a list of user accounts.

Any ideas?

Thank you !



<?php

require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
 
$clientSecret = "xxxxxxxxxxxx";
$callbackUrl = "http://www.xxxx.com";

function GetOAuth2Credential($user, $callbackUrl) {
try{
$redirectUri = $callbackUrl;
$offline = TRUE;
$extra_para = array('approval_prompt' => 'force');
// Get the authorization URL for the OAuth2 token.
// Pass in a redirect URL back to the application,
// Passing true for the second parameter ($offline) will provide us a refresh
// token which can used be refresh the access token when it expires.
$OAuth2Handler = $user->GetOAuth2Handler();
$authorizationUrl = $OAuth2Handler->GetAuthorizationUrl($user->GetOAuth2Info(), $redirectUri, $offline, $extra_para);
}
catch(Exception $e)
{
print_r($e->getMessage());
}
return $authorizationUrl;
}

if ($_REQUEST['code']){
$authCode = $_REQUEST['code'];
// --------------------
// Generate
// --------------------
$params = array(
"code" => $authCode,
"client_id" => $clientId,
"client_secret" => $clientSecret,
"redirect_uri" => $callbackUrl,
"grant_type" => "authorization_code"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
$result = json_decode ($curlData);
// print_r($result);
$access_token = $result->access_token;
$refresh_token = $result->refresh_token;
$token_type = $result->token_type;
$expires_in = $result->expires_in;
echo "<h3 style=\"padding:0; margin:0;\">General informations</h3>";
echo "<strong>My client id --></strong> ".$clientId;
echo "<br><strong>My secret key --></strong> ".$clientSecret;
echo "<br><strong>Callback url --></strong> ".$callbackUrl;
echo "<br><br>";
echo "<h3 style=\"padding:0; margin:0;\">Autorisation code</h3>";
echo "<strong>Auth code --></strong> ".$authCode;
echo "<br><br>";
echo "<h3 style=\"padding:0; margin:0;\">Generate auth token</h3>";
echo "<strong>Access token --></strong> ".$access_token;
echo "<br><strong>Refresh token --></strong> ".$refresh_token;
echo "<br><strong>Token type --></strong> ".$token_type;
echo "<br><strong>Expires in --></strong> ".$expires_in;
echo "<br><br><br><br>";
// --------------------
// I need to access user data. Whats next ?
// --------------------
}
else
{
// Create a new user and set the oAuth settings
$user = new AdWordsUser();
$user->LogAll();
$user->SetOAuth2Info(array(
"client_id" => $clientId,
"client_secret" => $clientSecret
));
// Generate an authorization URL given the callback URL
$authUrl = GetOAuth2Credential($user, $callbackUrl);
//header("Location:". $authUrl);
echo '<a href="'.$authUrl.'">Start process</a>';

}
?>

Umesh Dengale

unread,
Oct 9, 2015, 4:07:51 PM10/9/15
to AdWords API Forum
Hello,

The user has to grant access to their manager account(MCC) to get access to their AdWords accounts information. Please go through the OAuth2.0 for Web Server Applications guide for more details.

Thanks,
Umesh, AdWords API Team.

etienne....@admobio.com

unread,
Oct 13, 2015, 3:14:15 PM10/13/15
to AdWords API Forum
Hi Umesh and thank you for your response.

looked at the link you sent me and in fact the code I have included in my original publication does just that. The visitor is sent to an access request page to the Google server and the visitor is sent back to my page with an authorization code.

I use this snippet of code to request access to the account of the visitor:

$ user = new AdWordsUser ();
$ user
-> LogAll ();
$ user
-> SetOAuth2Info (array (
"client_id" => $ clientId,
"client_secret" => $ clientSecret
));


// Generate an authorization callback URL Given the URL
authUrl
GetOAuth2Credential $ = ($ user, $ callbackUrl);
// header ("Location:" $ authUrl.);
echo
'<a href="'.$authUrl.'"> </a> Start process';


Look at my complete code in the original publication.

My code, I think is good because it returns an authorization code. I don't understand what I am doing wrong.

I am open to any proposal.

Thank you !

Josh Radcliff (AdWords API Team)

unread,
Oct 14, 2015, 9:22:46 AM10/14/15
to AdWords API Forum
Hi,

Is the issue that:

a. You aren't getting back the OAuth credential from your GetOAuth2Credential call?

OR

b. You are getting back the OAuth credential, but you're not sure what to do next?

If a), then I'd recommend going through the PHP library's OAuth2 guide.

If b), then the Making Your First Request guide has some helpful instructions for each client library. Just make sure you click the PHP tab, and the guide will show you how to configure the library and make an API request.

Also, you mentioned that you wanted to get account information based on the OAuth credentials. Each user is associated with only one AdWords account (either a Manager Account or a regular AdWords account), and you can get that account's information by issuing a CustomerService.get call. This will give you back the clientCustomerId (among other things) of the account associated with your OAuth credentials.

Hope that helps!

Cheers,
Josh, AdWords API Team

etienne....@admobio.com

unread,
Oct 14, 2015, 11:53:06 AM10/14/15
to AdWords API Forum
Hi and thank you for your answer.

My problem was that I did not request to get the customer Id based on the OAuth credentials.

Now I have this in my code which allows me to retrieve the customer id:


$user = new AdWordsUser();

$user
->SetOAuth2Info(array(
 
"client_id" => $clientId,
 
"client_secret" => $clientSecret,
 
"access_token" => $access_token,
 
"refresh_token" => $refresh_token
));

$customerService
= $user->GetService('CustomerService');
$cust
= $customerService->get();
$user
->setCustomerClientId( $cust->customerId );

For now I have everything in hand to move forward. Thank you very much for your useful information!

Thank you,
Etienne

asma batool

unread,
Jun 23, 2016, 3:46:34 AM6/23/16
to AdWords API Forum
Hi,

I am able to access customer id  using your code now i want to send invitation to user for that i am using this code  

 $managedCustomerService =
                $user->GetService('ManagedCustomerService', 'v201603');
 $customerService = $user->GetService("CustomerService");

$customer = $customerService->get();
           
            $linkOperations =array();
            $managecustomerlink =  new ManagedCustomerLink();
            $managecustomerlink->managerCustomerId = '5301609003';
            $managecustomerlink->clientCustomerId=$customer->customerId;
            $managecustomerlink->linkStatus='PENDING';
            $linkOperation = new LinkOperation();
            $linkOperation->operator = 'ADD' ;
            $linkOperation->operand = $managecustomerlink ;
            $linkOperations = array($linkOperation);

           // $managedCustomerService->mutateLink($linkOperations);
            $result = $managedCustomerService->mutateLink( $linkOperations );

But in result i am getting this error 
          [ManagedCustomerServiceError.NOT_AUTHORIZED @ operations[0]]

its a very big blockage of my work any one help me .

Thanks in advance

Josh Radcliff (AdWords API Team)

unread,
Jun 23, 2016, 9:44:44 AM6/23/16
to AdWords API Forum
Hi,

That error suggests that you are using OAuth credentials from an account that does not have access to the manager account. The OAuth credentials for each request should be as follows:
  • Extending an invitation (your example), getting pending invitations, or rescinding an invitation: OAuth credentials for a user with access to the manager account
  • Accepting an invitation ("Client accepts" in the guide): OAuth credentials for a user with access to the client account
Please double-check which user's credentials you are using and let me know if you still have trouble.

Thanks,
Josh, AdWords API Team

asma batool

unread,
Jun 24, 2016, 4:52:12 AM6/24/16
to AdWords API Forum
Hi,

Thanks for replying I am using Oauth credentials from the same gmail account which is using for MCC test account. I don't know whats the reason its not working.i will really great full if you will help me to resolve this issue

Thanks 

Josh Radcliff (AdWords API Team)

unread,
Jun 24, 2016, 2:28:59 PM6/24/16
to AdWords API Forum
Hi,

I think I found the issue: the manager account in that request is a test account (as you mentioned), but the client account is a production (non-test) account. Please see the Test accounts section of the managing accounts guide, particularly this paragraph:

Because test and production accounts cannot interact in any way, you cannot use a test account under your existing production manager account. To use test accounts, you'll need a new account hierarchy, with a test manager account as the root.

The restriction also goes in the other direction: you cannot add a production account under your test manager account.

Thanks,
Josh, AdWords API Team

asma batool

unread,
Jun 28, 2016, 4:56:02 AM6/28/16
to AdWords API Forum

Hi,

I used My production account  but issue is not resolved

Thanks

Josh Radcliff (AdWords API Team)

unread,
Jun 28, 2016, 5:40:29 AM6/28/16
to AdWords API Forum
Hi,

Could you send me the requestId from your most recent attempt?

Thanks,
Josh, AdWords API Team
Reply all
Reply to author
Forward
Message has been deleted
0 new messages