1

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)

  • "Account Not Found"

  • "Client is unauthorized to retrieve access tokens using this method."

What I have done so far:

  • The Adsense Management API is enabled.

  • Credentials Created: Service Account

  • Credentials Created: oAuth 2.0 Client ID

  • The GSuite account has access to APIs (includes "enabled api access" and "managed API client access")

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?

tmarois
  • 2,424
  • 2
  • 31
  • 43

1 Answers1

2

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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks for the explanation and the sample code! I spent my entire day yesterday working on a lot of these outdated php examples, some work, some didn't, so I was really frustrated when I wrote this question up. I was able to get OAuth successfully working, as it turns out, the "account not found" really was an issue with not including "pub-" in the client id field, among other issues. The lack of documentation/examples and proper error messages is really the problem to the official php API. I appreciate the response. Since your answer clarifies my questions, I'll mark it as answered. Thanks! – tmarois Feb 22 '18 at 23:23
  • let me know if there's is anything I can do to improve my samples happy to hear that you got it working – Linda Lawton - DaImTo Feb 23 '18 at 06:23