PHP Adwords API - refresh_token authorization

84 views
Skip to first unread message

nnik

unread,
Jun 4, 2018, 2:02:00 PM6/4/18
to AdWords API and Google Ads API Forum
Hi folks, 

I want to ask the adwords api to request my campaigns.

I am using this simple example:

$code = $_GET['code'];

$oauth2 = new OAuth2([
    'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
    'redirectUri' => $redirectUri,
    'clientId' => $this->client_id,
    'clientSecret' => $this->client_secret,
    'scope' => '****'
]);

$oauth2->setCode($code);
$authToken = $oauth2->fetchAuthToken();

// Store the refresh token for your user in your local storage if you
// requested offline access.
$refreshToken = $authToken['refresh_token'];
Now I have the access_token and the refresh_token.

How can I use this to request google adwords now? When I am checking this example: 
https://developers.google.com/adwords/api/docs/samples/php/basic-operations

The authentication data is coming from some local file, but I have the data in my array above and the tokens are coming from the DB. 

Thanks
Nik

Bharani Cherukuri (AdWords API Team)

unread,
Jun 4, 2018, 4:47:43 PM6/4/18
to AdWords API and Google Ads API Forum
Hello Nik, 

For the PHP client library, it is highly recommended that you use adsapi_php.ini file to set the OAuth2 credentials. If you don't want to or can't use one, you can use the OAuth2 token and ads session builders instead to set the same information. Please check this guide for more information. Once you have these credentials set up, you will be able to run the code samples listed here

Let me know if you have any other questions. 

Regards,
Bharani, AdWords API Team

Peter Bowen

unread,
Jun 5, 2018, 4:28:41 AM6/5/18
to AdWords API and Google Ads API Forum
Hi Nik.

The example code looks like this:
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();


You should change that to look like this if you want to pass your OAuth2 credentials in from a db instead of from the file.

Enter code here...$oAuth2Credential = (new OAuth2TokenBuilder())
        ->withClientId(your oauth2 client id here)
        ->withClientSecret(your oauth2 client secret here)
        ->withRefreshToken(your stored refresh token here)
        ->build();

Reply all
Reply to author
Forward
Message has been deleted
0 new messages