【发布时间】:2016-02-16 22:09:42
【问题描述】:
我正在构建一个将图像上传到谷歌驱动器的 python 应用程序。然而,在工作了一段时间后,我的谷歌驱动器上传突然停止工作。每当我尝试初始化服务时,程序都会以代码 -1073740777 (0xC0000417) 退出。
我已经尝试使用开发者控制台(也使用完全不同的 Google 帐户)创建新的 client_secret.json 文件并删除 drive-python-quickstart.json 凭据文件。
我的朋友使用相同的代码没有这个问题,正如我所说,这对我也有用了一段时间,但突然停止工作。
我正在运行带有 Python 3.5 32 位的 Windows 10 Pro x64。
运行此示例程序时出现问题(取自the Google quickstart guide):
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
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,
'drive-python-quickstart.json')
store = oauth2client.file.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():
"""Shows basic usage of the Google Drive API.
Creates a Google Drive API service object and outputs the names and IDs
for up to 10 files.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
results = service.files().list(maxResults=10).execute()
items = results.get('items', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['title'], item['id']))
if __name__ == '__main__':
main()
【问题讨论】:
标签: python oauth-2.0 google-drive-api