簡體   English   中英

使用 PHP 創建文件夾 Google Drive API

[英]Create folder Google Drive API with PHP

最近我在谷歌驅動器上建立了一個自動文件創建系統,我已經將 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."
}
}

我真的不明白這是從哪里來的,因為我知道我已經設置了文檔中指示的 scope: $client->addScope(Google_Service_Drive::DRIVE);

我的 function:

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]

同樣的錯誤。 然后我檢查了 token.json,在“范圍”它是只讀的。 我已刪除該令牌並再次請求確保它在提供權限時具有 select 的“查看、編輯、創建和刪除所有 Google Drive 文件”選項,它會起作用。

這條線是 createFolder 的問題。 $client->setAccessToken('ya29.a0AfH6SMDO-L_7Lp6YWbGtSqWdPHJKCNezQr8-RRgS8xslHInLApC9uxBJVVljPTKEBgR-iidqIorjNaBThU4-aPjuAth1aD7mzjJXn5n2xP1xPw40p_OLssC7Ttj8CpBJHuqsUm89CYWAGL8cCHGhQ2hBY0YjKQ');

谷歌

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM