phpgoogle-apigoogle-oauthgoogle-api-php-clientadsense-api

Can not access the Adsense Management API


Can not access the AdSense API no matter what I try. I have tried both service account and OAuth authentications.

What I am trying to accomplish:

Creating a bot that will grab the Adsense revenue earned and store it in an in-house database for tracking revenue over time. I do not want to have authentication issues, I want a server-to-server configuration.

What I do know is you can have access tokens and refresh tokens under OAuth. I've yet to be able to make them work under any available APIs with Adsense. Note: I have built an API to work with AdWords no problem.

Errors: (Between the types of authentication, I can not pass these)

What I have done so far:

Libraries (trying) to use:

https://developers.google.com/api-client-library/php/

https://github.com/google/google-api-php-client

https://github.com/google/google-api-php-client-services

(outdated) https://github.com/googleads/googleads-adsense-examples

Example code: (using the service account)

$client = new \Google_Client();
$client->setAuthConfig(WRITEPATH . 'auth/adsense-client.json');
$client->setIncludeGrantedScopes(true);
$client->addScope('https://www.googleapis.com/auth/adsense');
$client->setSubject('email@example.com');

// trying to fire the services
$service = new \Google_Service_AdSense($client);
$report  = $service->accounts_reports->generate('clientId', $startDate, $endDate, $optParams);

This script fires the "Client is unauthorized to retrieve access tokens using this method." error message. However, the email address being used is able to access the adsense account, the APIs are enabled and configured on the admin domain level.

Is there an easier way? is there anyone who can lead me in the right direction?


Solution

  • Service account

    I am not all that sure that Service accounts work with adsence. You can try by going to the adsence website and under manageUsers add the service account email address. The issue here is its going to sit there at pending i think because the service account needs to confirm that it has been added. There is no way to do that.

    Admin domain level

    If you are trying to add domain wide delegation to this i wouldn't bother with that either as far as i can see its not supported either.

    Client is unauthorized to retrieve access tokens using this method.

    As for that error i may be able to help. It means that you have taken the credentials file you downloaded for a service account and are trying to use it with the code for Oauth2. or visa versa. Download the credentials file again and try again.

    I have some sample code that may help here