5

PyDrive を使用してファイルを google-drive-folder に正常にアップロードしています。しかし、私と共有されている google-drive-teamdrive-folder 内のフォルダーにファイルをアップロードする場合、次のコードは機能しません。

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)


location_to_save = "D:\images"
mImageLoc =  location_to_save + "\\abcd.jpg"

#[...Code to fetch and save the file as abcd.jpg ...]

gfolder_id = "1H1gjBKcpiHJtnXKVxWQEC1CS8t4Gswjj"  #This is a google drive folder id. I am replacing this with a teamdrive folder id, but that does not work
gfile_title = mImageLoc.split("\\")[-1] # returns abcd.jpg

http = gdrive.auth.Get_Http_Object()
f = gdrive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": gfolder_id}],
                                   'title': gfile_title})
            f.SetContentFile(mImageLoc)
            f.Upload(param={"http": http})

表示されるエラー メッセージは次のとおりです。pydrive.files.ApiRequestError: <HttpError 404 when requesting https://www.googleapis.com/upload/drive/v2/files?alt=json&uploadType=resumable returned "File not found: 0AG-N4DqGC1nbUk9PVA"> 「0AG-N4DqGC1nbUk9PVA」は、チームドライブのフォルダー ID です。

PyDrive を使用してチームドライブにファイルをアップロードする手段を探していましたが、無駄でした。pydrive の github ページを見ると、チームドライブのサポートが約 8 か月前に追加されたことがわかります。しかし、それを使用する方法に関するドキュメントが見つかりません。誰かが私が間違っている場所を提案できますか?

4

1 に答える 1

5

アップロードするには、 https ://pythonhosted.org/PyDrive/oauth.html の手順に従って、「settings.yaml」というファイルを作成し、作業ディレクトリに保存してみて ください。

client_secrets.json ファイルにあるクライアント ID とクライアント シークレットが必要になります。このファイルは、Google API へのアクセスを承認した後、ディレクトリにもあるはずです。

次のコードでテストして、チーム ドライブのフォルダーにテキスト ファイルを作成します。

parent_folder_id = 'YYYY'

f = drive.CreateFile({
    'title': 'test.txt',
    'parents': [{
        'kind': 'drive#fileLink',
        'teamDriveId': team_drive_id,
        'id': parent_folder_id
    }]
})
f.SetContentString('Hello World')

f.Upload(param={'supportsTeamDrives': True})

# where XXXX and YYYY are the team drive and target folder ids found from the end of the URLS when you open them in your browser.
于 2019-03-08T14:41:06.237 に答える