久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 python 的驅(qū)動(dòng)器 API 將文件上傳到谷歌驅(qū)

How do I upload file to google drive using drive API using python?(如何使用 python 的驅(qū)動(dòng)器 API 將文件上傳到谷歌驅(qū)動(dòng)器?)
本文介紹了如何使用 python 的驅(qū)動(dòng)器 API 將文件上傳到谷歌驅(qū)動(dòng)器?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想使用它的 API 將文件上傳到谷歌驅(qū)動(dòng)器,我正在使用代碼

I want to upload a file to google drive using its API, I am using the code

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

但是,我收到如下響應(yīng)錯(cuò)誤消息.

However, I am getting response error message as below.

{錯(cuò)誤":{錯(cuò)誤":[{域":全球",原因":authError","message": "此 API 不支持 HTTP 基本身份驗(yàn)證",位置類型":標(biāo)題","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" } }

還有其他方法可以使用 python 完成我的工作嗎?

Is there some other way to do my job using python.

我是否需要登錄谷歌云才能訪問(wèn) API 以獲取身份驗(yàn)證令牌或憑據(jù)

Should I need to sign in to google cloud to access API for authentication token or credentials

推薦答案

終于明白如何使用api上傳文件到google drive了.

Finally I Understood how do I upload file to google drive using api.

首先你需要安裝 python 庫(kù),它提供了使用驅(qū)動(dòng) api 的方法.安裝庫(kù): pip install google-api-python-client然后代碼如下.

first you need to install python library which gives the methods to use drive api. installing the library: pip install google-api-python-client then code as below.

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()))

上面的代碼片段是創(chuàng)建對(duì)象/變量,允許您使用正確的憑據(jù)進(jìn)入驅(qū)動(dòng)器.這里 drive_service 可以做到這一點(diǎn).

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.

文件上傳代碼如下.

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'))

文件 ID 很重要,因?yàn)槿绻獜尿?qū)動(dòng)器下載文件,則需要文件 ID.

The file ID is important because if you want to download the file from the drive you need the file ID.

這篇關(guān)于如何使用 python 的驅(qū)動(dòng)器 API 將文件上傳到谷歌驅(qū)動(dòng)器?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問(wèn)題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 国产一区中文字幕 | 日韩二区三区 | 99视频网站| 久久男人天堂 | 亚洲高清视频在线观看 | 国产色| 日本一二三区电影 | 国产伦一区二区三区久久 | www.国产一区 | 日韩中文一区二区三区 | 国产精品久久视频 | 亚洲精品久久久 | 免费av观看 | 日韩国产中文字幕 | 亚洲欧美综合精品久久成人 | 久久激情五月丁香伊人 | а天堂中文最新一区二区三区 | 久久久亚洲一区 | 久久精品国产免费 | 男人天堂久久 | 亚洲成人黄色 | 亚洲美女在线一区 | 在线观看特色大片免费网站 | 神马久久春色视频 | 一区二区免费在线视频 | 色婷婷影院 | 亚洲精品久久久一区二区三区 | 高清人人天天夜夜曰狠狠狠狠 | 七七婷婷婷婷精品国产 | 日韩av一区二区在线观看 | 成人三级网址 | 久久精品国产99国产精品 | 亚洲成人一区 | 午夜精品一区二区三区免费视频 | 一级片免费观看 | 欧美精品一区二区在线观看 | 日韩精品在线免费观看 | 日本不卡一区 | 午夜精品在线观看 | 日本精品一区二区三区视频 | 中文字幕国产高清 |