Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine Google API PHP客户端-PHP致命错误:未捕获异常';无效辩论例外';带有消息';无效代码';_Google App Engine_Google Api Php Client - Fatal编程技术网

Google app engine Google API PHP客户端-PHP致命错误:未捕获异常';无效辩论例外';带有消息';无效代码';

Google app engine Google API PHP客户端-PHP致命错误:未捕获异常';无效辩论例外';带有消息';无效代码';,google-app-engine,google-api-php-client,Google App Engine,Google Api Php Client,我已经把我的php应用程序上传到谷歌应用程序引擎上了。它以计划任务或cron作业的形式出现。有人能帮我吗 我在日志中收到以下错误: PHP致命错误:未捕获异常“InvalidArgumentException”,在/base/data/home/apps//google api PHP client/src/google/client.PHP:168中显示消息“Invalid code” 这是我的密码: function uploadData ($responseData){ require

我已经把我的php应用程序上传到谷歌应用程序引擎上了。它以计划任务或cron作业的形式出现。有人能帮我吗

我在日志中收到以下错误:

PHP致命错误:未捕获异常“InvalidArgumentException”,在/base/data/home/apps//google api PHP client/src/google/client.PHP:168中显示消息“Invalid code”

这是我的密码:

function uploadData ($responseData){

require __DIR__ . '/google-api-php-client/vendor/autoload.php';

define('APPLICATION_NAME', 'Drive API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
        Google_Service_Drive::DRIVE_FILE)
));

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfigFile(CLIENT_SECRET_PATH);
    $client->setAccessType('offline');

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
        $accessToken = file_get_contents($credentialsPath);
    } else {
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        printf("Open the following link in your browser:\n%s\n", $authUrl);
        print 'Enter verification code: ';
        $authCode = trim(fgets(STDIN));

        // Exchange authorization code for an access token.
        $accessToken = $client->authenticate($authCode);

        // Store the credentials to disk.
        if(!file_exists(dirname($credentialsPath))) {
            mkdir(dirname($credentialsPath), 0700, true);
        }
        file_put_contents($credentialsPath, $accessToken);
        printf("Credentials saved to %s\n", $credentialsPath);
    }
    $client->setAccessToken($accessToken);

    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, $client->getAccessToken());
    }
    return $client;
}

/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path) {
    $homeDirectory = getenv('HOME');
    if (empty($homeDirectory)) {
        $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
    }
    return str_replace('~', realpath($homeDirectory), $path);
}


// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);

$title = 'ordersMonthly30Days';

$file = new Google_Service_Drive_DriveFile($client);
$file->setTitle($title);

/*
$result = $service->files->insert($file, array(
    'data' => $responseData,
    'mimeType' => 'application/octet-stream',
    'uploadType' => 'media',
    'convert' => true,
));*/

$result = updateFile($service,'file id',$title,'testing update','application/octet-stream',$responseData,true);
}

/**
* Update an existing file's metadata and content.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param string $fileId ID of the file to update.
* @param string $newTitle New title for the file.
* @param string $newDescription New description for the file.
* @param string $newMimeType New MIME type for the file.
* @param string $newFilename Filename of the new content to upload.
* @param bool $newRevision Whether or not to create a new revision for this     file.
* @return Google_Servie_Drive_DriveFile The updated file. NULL is returned   if
*     an API error occurred.
*/
function updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) {
try {
    // First retrieve the file from the API.
    $file = $service->files->get($fileId);

    // File's new metadata.
    $file->setTitle($newTitle);
    $file->setDescription($newDescription);
    $file->setMimeType($newMimeType);

    // File's new content.
    $data = $newFileName;

    $additionalParams = array(
        'newRevision' => $newRevision,
        'data' => $data,
        'mimeType' => $newMimeType
    );

    // Send the request to the API.
    $updatedFile = $service->files->update($fileId, $file, $additionalParams);
    return $updatedFile;
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}
}

对于这个问题的未来读者,本例中错误的原因是在读写凭证文件时需要对JSON进行解码和编码,因为该文件是JSON格式的。当时可能还不清楚该怎么做,但展示了如何使用
json_decode()
json_encode()
来处理这个问题

如果在调用PHP API客户端函数时遇到任何类型的
InvalidArgumentException
(或任何其他异常),请记住该函数实际上返回了错误的详细信息,因此可以捕获
InvalidArgumentException
,并对结果使用
var_dump()
要查看实际错误是什么,如下所示:

try {
    // ...
    $accessToken = $client->authenticate($authCode);
    // ...
}
catch (InvalidArgumentException $e) {
    var_dump($accessToken)
}
试试看。当您第一次运行quickstart.php文件时。文件any_file_name.json将动态创建。有关存储基本信息和凭据的信息,请参见我的回答:问候
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
replace the above line with 
define('CREDENTIALS_PATH', '/any_file_name.json');