Google App Engine を使用して、ファイル「a.txt」を Google ドライブに挿入しようとしています。InsertDriveページのページソースを表示すると発生するエラーは
HttpError 401 "ログインが必要です" 0x10f884b0 の main.InsertDrive オブジェクトのバインドされたメソッド InsertDrive.error
注: MainHandler クラスの Jinja テンプレートに URL を表示することで、MainHandler クラスからクラス InsertDrive を呼び出しています。
import httplib2
import logging
import os
import sys
from os import path
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
from apiclient import discovery
from oauth2client import appengine
from oauth2client import client
from google.appengine.api import memcache
from apiclient import errors
from apiclient.http import MediaFileUpload
import webapp2
import jinja2
CREDENTIAL = 'drive.credential'
CLIENT_SECRET_JSON = 'client_secrets.json'
SCOPE = 'https://www.googleapis.com/auth/drive'
FILE_NAME = 'a.txt'
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
autoescape=True,
extensions=['jinja2.ext.autoescape'])
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
MISSING_CLIENT_SECRETS_MESSAGE = """
Warning: Please configure OAuth 2.0
""" % CLIENT_SECRETS
http = httplib2.Http(memcache)
service = discovery.build('drive', 'v2', http=http)
decorator = appengine.oauth2decorator_from_clientsecrets(
CLIENT_SECRETS,
scope=[
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.apps.readonly',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.scripts',
],
message=MISSING_CLIENT_SECRETS_MESSAGE)
title="a.txt"
description="none"
mime_type="text/*"
filename="a.txt"
parent_id=None
class MainHandler(webapp2.RequestHandler):
@decorator.oauth_aware
def get(self):
insert_url = "/InsertDrive"
if not decorator.has_credentials():
url = decorator.authorize_url()
self.redirect(url)
self.response.write("Hello")
#variables = {
# 'url': decorator.authorize_url(),
# 'has_credentials': decorator.has_credentials(),
# 'insert_url': "/InsertDrive"
# }
template = JINJA_ENVIRONMENT.get_template('main.html')
self.response.write(template.render(insert_url=insert_url))
class InsertDrive(webapp2.RequestHandler):
# ADDED FUNCTION TO UPLOAD #
def get(self):
self.response.out.write('<h1>entered</h1>')
media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
self.response.write(media_body)
body = {
'title': title,
'description': description,
'mimeType': mime_type
}
self.response.write(body)
# Set the parent folder.
if parent_id:
body['parents'] = [{'id': parent_id}]
self.response.write(parent_id)
try:
file = service.files().insert(
body=body,
media_body=media_body).execute()
self.response.write(file)
# Uncomment the following line to print the File ID
# print 'File ID: %s' % file['id']
except errors.HttpError , error:
self.response.write('<h1>checking if error</h1>: %s' % error)
self.response.write(self.error)
print 'An error occured: %s' % error
app = webapp2.WSGIApplication(
[
('/', MainHandler),
('/InsertDrive' , InsertDrive),
(decorator.callback_path, decorator.callback_handler()),
],
debug=True)
どんな助けでも大歓迎ですありがとう、kira_111