简体   繁体   English

如何使用 python 使用驱动器 API 将文件上传到谷歌驱动器?

[英]How do I upload file to google drive using drive API using python?

I want to upload a file to google drive using its API, I am using the code我想使用其 API 将文件上传到谷歌驱动器,我正在使用代码

def newer():
    url= 'https://USERNAME:PASSWORD@www.googleapis.com/upload/drive/v3/files?uploadType=media'
    data='''{{
      "name":"testing.txt",
    }}'''
    response = requests.post(url, data=data)
    print response.text

However, I am getting response error message as below.但是,我收到如下响应错误消息。

{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "HTTP Basic Authentication is not supported for this API", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "HTTP Basic Authentication is not supported for this API" } } { "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "HTTP Basic Authentication is not supported for this API", "locationType": "header ", "location": "Authorization" } ], "code": 401, "message": "HTTP Basic Authentication is not supported for this API" } }

Is there some other way to do my job using python.有没有其他方法可以使用 python 完成我的工作。

Should I need to sign in to google cloud to access API for authentication token or credentials我是否需要登录 Google Cloud 才能访问 API 以获取身份验证令牌或凭据

Finally I Understood how do I upload file to google drive using api.最后我明白了如何使用 api 将文件上传到谷歌驱动器。

first you need to install python library which gives the methods to use drive api.首先你需要安装 python 库,它提供了使用驱动器 api 的方法。 installing the library: pip install google-api-python-client then code as below.安装库: pip install google-api-python-client 然后代码如下。

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.http import MediaFileUpload,MediaIoBaseDownload
import io

# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
drive_service = build('drive', 'v3', http=creds.authorize(Http()))

above code snippet is to create object/variable which allow you to get inside the drive with a right credential.上面的代码片段是创建对象/变量,允许您使用正确的凭据进入驱动器。 here drive_service does that work.在这里drive_service完成这项工作。

File uploading code is below here.文件上传代码如下。

def uploadFile():
    file_metadata = {
    'name': 'fileName_to_be_in_drive.txt',
    'mimeType': '*/*'
    }
    media = MediaFileUpload('Filename_of_your_local_file.txt',
                            mimetype='*/*',
                            resumable=True)
    file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    print ('File ID: ' + file.get('id'))

The file ID is important because if you want to download the file from the drive you need the file ID.文件 ID 很重要,因为如果要从驱动器下载文件,则需要文件 ID。

In order to access private user data you need the permission of the user.为了访问私人用户数据,您需要获得用户的许可。 You cant upload to my drive account without my permission.未经我的许可,您不能上传到我的驱动器帐户。

USERNAME:PASSWORD用户名密码

Is called basic authentication and uses login and password google shut down for this in 2015.称为基本身份验证,并使用 2015 年谷歌为此关闭的登录名和密码。

In order to access private user data now you will need to use Oauth2.为了现在访问私人用户数据,您需要使用 Oauth2。

I suggest starting with the Python quickstart我建议从Python 快速入门开始

"""
Shows basic usage of the Drive v3 API.

Creates a Drive v3 API service and prints the names and ids of the last 10 files
the user has access to.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))

# Call the Drive v3 API
results = service.files().list(
    pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print('{0} ({1})'.format(item['name'], item['id']))

Simple Way简单的方法

First, install the google drive library using:首先,使用以下命令安装谷歌驱动器库:

!pip install gdown

Load data加载数据

Copy your google drive (ensure you have the correct google link):复制你的谷歌驱动器(确保你有正确的谷歌链接):

!gdown https://test_google_url.com

If required, unzip the file:如果需要,解压缩文件:

!unzip test_file_here.zip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 驱动器 api 将 csv 文件上传到谷歌驱动器 - How to upload csv files to google drive using python drive api 如何使用python Telegram Bot API将文件上传到Google驱动器 - How to upload a file to google drive using python Telegram Bot API 如何使用 Google 云端硬盘 API v3 向 Python 中的共享文件夹进行可续传上传? - How do I do a resumable upload to a shared folder in Python using Google Drive API v3? 如何使用 Python 和 Drive API v3 将文件上传到 Google Drive - How to upload a file to Google Drive using Python and the Drive API v3 如何使用 Python 通过 api 将文件上传到谷歌驱动器? - How to upload files into google drive via api using Python? 如何使用 python 请求使用谷歌驱动器 API 上传和删除文件 - How to upload and delete files with google drive API using python requests 如何使用Google Drive API v3上传到Python中的共享驱动器? - How do I upload to a shared drive in Python with Google Drive API v3? 如何使用Python脚本将文件上传到Google云端硬盘? - How to upload a file to Google Drive using a Python script? 如何使用python代码将Android上的文件上传到Google云端硬盘 - How to upload file on android to Google Drive using python code 如何使用python将csv文件上传和下载到谷歌驱动器? - How to upload and download a csv file to google drive using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM