Search code examples
adsense-api

google Api adsense (401) Invalid Credentials


I have AdSense account with custom channels on it I make every thing but there are two problems:

  1. I get reports for all account i need to get reports for one channel based on channel id
  2. I need to get reports for all users without they have my google to access whene i save my details in database

My data:

{
  "access_token" : "XXXX.XXXX",
  "token_type" : "XXXX",
  "expires_in" : XXXX,
  "created" : XXXX
}

My update statements:

$access_token_update = json_decode($access_token);
$access_token_update->created = time();

I'm getting this error message:

(401) Invalid Credentials

My code:

$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setAccessType('offline');
$client->setAccessToken($access_token);
$service= new Google_Service_AdSense($client);

Solution

  • do steps in this video

    https://www.youtube.com/watch?v=hfWe1gPCnzc

    save code from results example

    {
     "access_token": "XXXXXXXX", 
     "token_type": "XXXX", 
     "expires_in": XXXX, 
     "refresh_token": "XXXXXXXXX"
    

    }

    then update your file as the following

    $access_token = "from database or file or what you want to save"; // this will be code you got by doing steps in this video.
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setAccessType('offline');
    $access_token_json = json_decode($access_token);
    $client->refreshToken($access_token_json ->refresh_token);
    $service= new Google_Service_AdSense($client);
    

    enjoy :) thanks