用PHP创建文件夹googledriveapi

用PHP创建文件夹googledriveapi,php,google-drive-api,Php,Google Drive Api,最近,我在谷歌硬盘上建立了一个自动文件创建系统,我已经将API集成到我的symfony应用程序中,我设法对自己进行身份验证,等等,但我没有阻止创建文件的步骤 出现以下错误: { "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message"

最近,我在谷歌硬盘上建立了一个自动文件创建系统,我已经将API集成到我的symfony应用程序中,我设法对自己进行身份验证,等等,但我没有阻止创建文件的步骤

出现以下错误:

{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission: Request had insufficient authentication scopes."
}
],
"code": 403,
"message": "Insufficient Permission: Request had insufficient authentication scopes."
}
}
我真的不明白这是从哪里来的,因为我知道我已经在文档中设置了范围:
$client->addScope(Google\u Service\u Drive::Drive)

我的职能:

public function createFolder($name)
    {

        $client = $this->getClient();
//        $client->setAuthConfig('code_secret_client.json');
        $client->addScope(Google_Service_Drive::DRIVE);
        $client->setAccessToken('ya29.a0AfH6SMDO-L_7Lp6YWbGtSqWdPHJKCNezQr8-RRgS8xslHInLApC9uxBJVVljPTKEBgR-iidqIorjNaBThU4-aPjuAth1aD7mzjJXn5n2xP1xPw40p_OLssC7Ttj8CpBJHuqsUm89CYWAGL8cCHGhQ2hBY0YjKQ');

        $service = new Google_Service_Drive($client);
        $folder = new Google_Service_Drive_DriveFile();

        $folder->setName($name);
        $folder->setMimeType('application/vnd.google-apps.folder');

        $result = $service->files->create($folder);
        dump($result);
        return $result;
    }

你有什么想法吗?

我可以通过谷歌的例子重现

<?php
/**
 * Copyright 2018 Google Inc.
 *
 * 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
 *
 *     http://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.
 */
// [START drive_quickstart]
require __DIR__ . '/vendor/autoload.php';

if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
}

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient()
{
    $client = new Google_Client();
    $client->setApplicationName('Google Drive API PHP Quickstart');
    $client->setScopes(Google_Service_Drive::DRIVE);
    $client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $client->setPrompt('select_account consent');

    // Load previously authorized token from a file, if it exists.
    // The file token.json stores the user's access and refresh tokens, and is
    // created automatically when the authorization flow completes for the first
    // time.
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
        $accessToken = json_decode(file_get_contents($tokenPath), true);
        $client->setAccessToken($accessToken);
    }

    // If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
        // Refresh the token if possible, else fetch a new one.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        } 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->fetchAccessTokenWithAuthCode($authCode);
            $client->setAccessToken($accessToken);

            // Check to see if there was an error.
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
            }
        }
        // Save the token to a file.
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        }
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }
    return $client;
}

      $name = "Stackoverflow";
 
       // Get the API client and construct the service object.
       $client = getClient();
 


        $service = new Google_Service_Drive($client);
        $folder = new Google_Service_Drive_DriveFile();

        $folder->setName($name);
        $folder->setMimeType('application/vnd.google-apps.folder');

        $result = $service->files->create($folder);
// [END drive_quickstart]


我可以通过谷歌的例子重现

<?php
/**
 * Copyright 2018 Google Inc.
 *
 * 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
 *
 *     http://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.
 */
// [START drive_quickstart]
require __DIR__ . '/vendor/autoload.php';

if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
}

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient()
{
    $client = new Google_Client();
    $client->setApplicationName('Google Drive API PHP Quickstart');
    $client->setScopes(Google_Service_Drive::DRIVE);
    $client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $client->setPrompt('select_account consent');

    // Load previously authorized token from a file, if it exists.
    // The file token.json stores the user's access and refresh tokens, and is
    // created automatically when the authorization flow completes for the first
    // time.
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
        $accessToken = json_decode(file_get_contents($tokenPath), true);
        $client->setAccessToken($accessToken);
    }

    // If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
        // Refresh the token if possible, else fetch a new one.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        } 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->fetchAccessTokenWithAuthCode($authCode);
            $client->setAccessToken($accessToken);

            // Check to see if there was an error.
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
            }
        }
        // Save the token to a file.
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        }
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }
    return $client;
}

      $name = "Stackoverflow";
 
       // Get the API client and construct the service object.
       $client = getClient();
 


        $service = new Google_Service_Drive($client);
        $folder = new Google_Service_Drive_DriveFile();

        $folder->setName($name);
        $folder->setMimeType('application/vnd.google-apps.folder');

        $result = $service->files->create($folder);
// [END drive_quickstart]


为什么您要评论setAuthConfigAh抱歉,我必须为测试添加注释,但即使不是注释,我仍然有相同的错误:/此用户没有创建文件夹的权限。检查是admin还是ownera这是我的个人帐户,我想我对它拥有所有的权利,不是吗?你在使用这个例子:为什么你评论了setAuthConfigAh对不起,我不得不评论一个测试,但即使它不是评论,我仍然有相同的错误:/这个用户没有创建文件夹的权限。检查是admin还是ownera这是我的个人帐户,我想我对它拥有所有的权利,不是吗?你正在使用这个示例: