在Tanaike的帮助下,我可以使用rest API创建文件夹。但我得到了python代码错误。错误是“ https://www.googleapis.com/drive/v3/files?fields=id&alt=json返回”权限不足“> ” 。这是我引用here的代码:
#!/usr/bin/python
#-*- coding:utf-8 -*-
import os
import httplib2
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.file'
]
CLIENT_SECRET_FILE = 'client_secret.json'
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'googleapis.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
# flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
file_metadata = {
'name': 'movie',
'mimeType':'application/vnd.google-apps.folder'
}
DRIVE = discovery.build("drive", "v3", http=http)
file = DRIVE.files().create(body=file_metadata, fields="id").execute()
print(file.get('id'))
if __name__ == '__main__':
main()
感谢。请帮帮我!
答案 0 :(得分:0)
我认为你的脚本有效。那又如何确认以下几点呢?
googleapis.json
中的.credentials
。
googleapis.json
后,再次运行脚本时,将重新创建googleapis.json
文件。此时,范围将反映到访问令牌。https://console.cloud.google.com/apis/library/drive.googleapis.com/?project=project-id-#####
确认。 “项目ID”是您检索的项目的ID client_secret.json
。如果这对你没用,我很抱歉。