kira_111
kira_111

Reputation: 95

Error in listing the google drive files

I am trying to list all my google drive files in an application using google app engine. I am using the oauth protocol to access the drive files. On clicking the link to list the files, i get an error, which is listed below.

Please help. Thanks in advance.

Here is my program

#!/usr/bin/env python
#
# Copyright 2013 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Starting template for Google App Engine applications.

Use this project as a starting point if you are just beginning to build a Google
App Engine project. Remember to download the OAuth 2.0 client secrets which can
be obtained from the Developer Console <https://code.google.com/apis/console/>
and save them as 'client_secrets.json' in the project directory.
"""

import httplib2
import logging
import os
from google.appengine.api import users
from apiclient import discovery
from oauth2client import appengine
from oauth2client import client
from google.appengine.api import memcache
import webapp2
import jinja2


JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    autoescape=True,
    extensions=['jinja2.ext.autoescape'])

# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
# application, including client_id and client_secret, which are found
# on the API Access tab on the Google APIs
# Console <http://code.google.com/apis/console>
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

# Helpful message to display in the browser if the CLIENT_SECRETS file
# is missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
<h1>Warning: Please configure OAuth 2.0</h1>
<p>
To make this sample run you will need to populate the client_secrets.json file
found at:
</p>
<p>
<code>%s</code>.
</p>
<p>with information found on the <a
href="https://code.google.com/apis/console">APIs Console</a>.
</p>
""" % 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/userinfo.email',
      'https://www.googleapis.com/auth/userinfo.profile',
      '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)

class MainHandler(webapp2.RequestHandler):
    @decorator.oauth_aware
    def get(self):
        user= users.get_current_user()
        if user:
            variables = {
            'user': users.get_current_user(),
                    'url': decorator.authorize_url(),
                'has_credentials': decorator.has_credentials()
            }
                template = JINJA_ENVIRONMENT.get_template('main.html')
                self.response.write(template.render(variables))
        else:
                self.response.write(users.create_login_url("/"))    

class listFile(webapp2.RequestHandler):
    @decorator.oauth_aware
    def get(self):
        if not decorator.has_credentials():
            self.reponse.write("<a href='")
            self.reponse.write(decorator.authorize_url())
            self.response.write(">Authorize</a>")
        else:
                a=service.files().list().execute()
                self.response.write(a)
app = webapp2.WSGIApplication(
    [
     ('/', MainHandler),
     ('/list', listFile),
     (decorator.callback_path, decorator.callback_handler()),
    ],
    debug=True)

The error i get is

Internal Server Error

The server has either erred or is incapable of performing the requested operation.

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~test-ak2/1.371630947154576708/oauth2client/appengine.py", line 777, in setup_oauth
    resp = method(request_handler, *args, **kwargs)
  File "/base/data/home/apps/s~test-ak2/1.371630947154576708/main.py", line 104, in get
    a=service.files().list().execute()
  File "/base/data/home/apps/s~test-ak2/1.371630947154576708/oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/base/data/home/apps/s~test-ak2/1.371630947154576708/apiclient/http.py", line 723, in execute
    raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 401 when requesting https://www.googleapis.com/drive/v2/files?alt=json returned "Login Required">

Upvotes: 1

Views: 736

Answers (1)

aeijdenberg
aeijdenberg

Reputation: 2457

When you are requesting the file list:

service.files().list().execute()

the client library needs to know how to authenticate the HTTP request. The way it does this is is uses a special HTTP object that knows how to add the right credentials to the request.

This can be set either at initialization of the service, or it can be specified when you are calling execute().

In your case you are specifying it when you initialize the service object:

http = httplib2.Http(memcache)
service = discovery.build('drive', 'v2', http=http)

That's fine - but when you use the service object, it doesn't know about the decorator, so what you need to do is get an HTTP object from the decorator:

service.files().list().execute(http=decorator.http())

and pass that to the execute() method. This will ensure the credentials associated with the logged in user are added to the request to the Drive API.

Upvotes: 1

Related Questions