Uncaught InvalidArgumentException: file does not exist in /vendor/google/apiclient/src/Google/Client.php:839

1,539 views
Skip to first unread message

nema vashe

unread,
Sep 24, 2020, 4:31:49 AM9/24/20
to AdMob API Developer Forum
Hello.
When i opening my code in browser all works fine.
But when i trying to start it in console, i got this:
root@:~# php PATH/TO/MY/SCRIPT/cron.php
PHP Fatal error:  Uncaught InvalidArgumentException: file does not exist in PATH/TO/MY/SCRIPT/vendor/google/apiclient/src/Google/Client.php:839
Stack trace:
#0  PATH/TO/MY/SCRIPT/cron.php(22): Google_Client->setAuthConfig('client_secrets....')
#1 {main}
  thrown in  PATH/TO/MY/SCRIPT/vendor/google/apiclient/src/Google/Client.php on line 839

Line 22 in my cron.php:
$client->setAuthConfig('client_secrets.json');

I tried to set full path to json file, but got another one error:
PHP Notice:  Undefined index: HTTP_HOST in  PATH/TO/MY/SCRIPT/cron.php on line 29
PHP Fatal error:  Uncaught InvalidArgumentException: Unable to parse URI: http:///PATH/TO/MY/SCRIPT/cron.php in  PATH/TO/MY/SCRIPT/vendor/guzzlehttp/psr7/src/Uri.php:71
Stack trace:
#0  PATH/TO/MY/SCRIPT/vendor/guzzlehttp/psr7/src/functions.php(59): GuzzleHttp\Psr7\Uri->__construct('http:///var/www...')
#1  PATH/TO/MY/SCRIPT/vendor/google/auth/src/OAuth2.php(1243): GuzzleHttp\Psr7\uri_for('http:///var/www...')
#2  PATH/TO/MY/SCRIPT/vendor/google/auth/src/OAuth2.php(1282): Google\Auth\OAuth2->coerceUri('http:///var/www...')
#3  PATH/TO/MY/SCRIPT/vendor/google/auth/src/OAuth2.php(727): Google\Auth\OAuth2->isAbsoluteUri('http:///var/www...')
#4  PATH/TO/MY/SCRIPT/vendor/google/auth/src/OAuth2.php(328): Google\Auth\OAuth2->setRedirectUri('http:///var/www...')
#5  PATH/TO/MY/SCRIPT/vendor/google/apiclient/src/Google/Client.php(932): Google\Auth\OAuth2->__construct(Array)
#6  PATH/TO/MY/SCRIPT/vendor/google/apiclient/src/Googl in PATH/TO/MY/SCRIPT/vendor/guzzlehttp/psr7/src/Uri.php on line 71

So, how can i start the script in console (cron)?

AdMob API Forum Advisor

unread,
Sep 30, 2020, 10:32:18 PM9/30/20
to dfk...@gmail.com, google-admob-api...@googlegroups.com

Hi Nema,

Thank you for your patience with this issue. I have created an example that can be run from the console to help solve this problem. To run the sample, follow the steps below:

  1. Complete all of the prerequisite steps .
  2. Copy the sample, admob_api_sample.php, below into the googleads-admob-api-samples/php/v1/ directory.
  3. Run the following command in the console.
     php admob_api_sample.php
    1. A link will be printed to the console to initiate the OAuth 2.0 flow. Click on the link.
    2. Copy your authorization code into the console.
  4. If the access token expires, delete the contents of tokens.dat file.
 

admob_api_sample.php

<?php

/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/************************************************
  ATTENTION: Change this path to point to your vendor folder if your project
  directory structure differs from this repository's!
 ************************************************/

require_once __DIR__ . '/vendor/autoload.php';

use Examples\Reports\GenerateMediationReport;
use Examples\Reports\GenerateNetworkReport;
use Examples\Accounts\ListAccounts;
use Examples\Accounts\GetAccount;

use function HtmlHelper\pageHeader;
use function HtmlHelper\pageFooter;

session_start();

// Configure token storage on disk.
// Store refresh tokens in a local disk file.
define('STORE_ON_DISK', true);
define('TOKEN_FILENAME', 'tokens.dat');

$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/admob.report');
$client->setApplicationName('AdMob API PHP Quickstart');
$client->setAccessType('offline');

// Be sure to replace the contents of client_secrets.json with your developer
// credentials.
$client->setAuthConfig('client_secrets.json');

$service = new Google_Service_AdMob($client);

// If we have an access token, we can make requests, else we generate an
// authentication URL.
if (
    STORE_ON_DISK && file_exists(TOKEN_FILENAME) &&
      filesize(TOKEN_FILENAME) > 0
) {
    $client->refreshToken(file_get_contents(TOKEN_FILENAME));
} else {
    // Begin the OAuth 2.0 flow by generating an authentication URL.
    $authUrl = $client->createAuthUrl();
    echo "Click on the " . $authUrl . " to retrieve your access code \n\n";
    $code = readline("Enter your access code: ");
 
    // Exchange code from OAuth 2.0 flow with the authenticate() function.
    $accessToken = $client->fetchAccessTokenWithAuthCode($code);
    $refreshToken = $client->getRefreshToken();

    if (STORE_ON_DISK) {
        file_put_contents(TOKEN_FILENAME, $refreshToken);
    }
}

echo 'AdMob API samples';

// Now we're signed in, we can make our requests.
makeRequests($service);

// Makes all the API requests.
function makeRequests($service)
{
   /*
    * The first account from the list of accounts is being used as input
    * for the following requests.
    *
    * For other accounts, replace 'account_name' below which follows the format
    * "accounts/pub-XXXXXXXXXXXXXXXX".
    * See https://support.google.com/admob/answer/2784578
    * for instructions on how to find your account name.
    */
    $accountName = ListAccounts::getFirstAccountName($service);

    if (isset($accountName)) {
        ListAccounts::run($service);
        GetAccount::run($service, $accountName);
        GenerateNetworkReport::run($service, $accountName);
        GenerateMediationReport::run($service, $accountName);
    } else {
        echo 'Please specify the account_name, which should follow a format of
            "accounts/pub-XXXXXXXXXXXXXXXX".
            See https://support.google.com/admob/answer/2784578
            for instructions on how to find your account name.';
    }
}

Please do not hesitate to reach out if you have any additional questions or concerns.

All the best,

Google Logo
Amira Badreldin
AdMob API Team
 


ref:_00D1U1174p._5004Q25WUen:ref

nema vashe

unread,
Oct 1, 2020, 4:46:14 AM10/1/20
to AdMob API Developer Forum
Hi, Amira.
Your code have some minor differences from mine, but more interesting is that your code cant authorize with my "tokens.dat", and at the same time my old code without any changes now run in console pretty well.
This is a miracle or mysticism. In any case, we can consider that the problem is solved.
четверг, 1 октября 2020 г. в 05:32:18 UTC+3, admobapiforumadvisor:

nema vashe

unread,
Nov 26, 2020, 11:05:30 AM11/26/20
to AdMob API Developer Forum
Hi there.
So.... Now i can`t run this script via CRON, because when it run - the main directory of script is is a directory of my crontab file. So the running script cant find file "client_secrets.json".
I tried to setup absolute path to file like this:
$client->setAuthConfig('/var/www/data/domain/client_secrets.json');  
but it crashes some Guzzle scripts, that uses Google library, cause he cant parse URL.
Please, help me! I tired to run this script manually every morning even weekends and holidays!
четверг, 1 октября 2020 г. в 05:32:18 UTC+3, admobapiforumadvisor:

Hi Nema,

AdMob API Forum Advisor

unread,
Dec 2, 2020, 1:34:31 AM12/2/20
to dfk...@gmail.com, google-admob-api...@googlegroups.com
Hello,

I will investigate the issue and provide a response as soon as possible. Thank you for bringing this to our attention.

AdMob API Forum Advisor

unread,
Dec 27, 2020, 9:37:43 PM12/27/20
to dfk...@gmail.com, google-admob-api...@googlegroups.com
Hello,

I have been unable to reproduce the issue locally, if possible, please privately share your script, environment setup and error so I can help resolve your issue.

nema vashe

unread,
Dec 30, 2020, 3:03:52 PM12/30/20
to AdMob API Developer Forum
Hello. How can i send all information privately?

понедельник, 28 декабря 2020 г. в 05:37:43 UTC+3, admobapiforumadvisor:

AdMob API Forum Advisor

unread,
Dec 31, 2020, 8:49:37 PM12/31/20
to dfk...@gmail.com, google-admob-api...@googlegroups.com
Hello,


To share details privately, please use the Reply privately to author option which can be found under the More menu in top right corner of the message.
Reply all
Reply to author
Forward
0 new messages