1

I am using python 3.4 to leverage the Google API to access and read files from a users google drive. If a user has already used the app before they should have a credentials file so I was hoping to be able to test if the credentials are still valid by attempting to list the files on the users drive. The idea being if this errors then the app knows it needs to ask for access again.

After a lot of searching I've tried to piece together code from the following Examples:
Google API commands
Google Example

I currently have the following pieces of code:

import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow

def getAccess():
    flow = OAuth2WebServerFlow(client_id, client_secret, scope,     redirect_uri="urn:ietf:wg:oauth:2.0:oob")
    auth_uri = flow.step1_get_authorize_url()
    print("Please go to the following webpage and copy and paste the access key onto the command line:\n" + auth_uri + "\n")
    code = input("Please enter your access code:\n")
    credentials = flow.step2_exchange(code)
    storage.put(credentials)

client_id = MYCLIENT_ID
client_secret = MYCLIENT_SECRET
scope = "https://www.googleapis.com/auth/drive"

storage = Storage('~/credentials.dat')
credentials = storage.get()

if credentials is None or credentials.invalid:
    getAccess()
else:
    try:
        http = httplib2.Http()
        http = credentials.authorize(http)
        service = build('drive', 'v2', http=http)
        param = {}
        service.files().list(**param).execute()
    except:
        getAccess()

However the service.files().list(**param).execute() line produces the following error message:

Traceback (most recent call last):
  File "GoogleAuth.py", line 64, in <module>
    service.files().list(**param).execute()
  File "C:\Anaconda3\lib\site-packages\oauth2client\util.py", line 137, in     positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Anaconda3\lib\site-packages\googleapiclient\http.py", line 729, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError

I've tried playing around with a few different combinations such as:

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

However I still get the same error message. Any idea what's going on ?

gowerc
  • 1,039
  • 9
  • 18
  • 1
    [An HttpError can contain a `reason`](https://github.com/google/google-api-python-client/blob/master/googleapiclient/errors.py#L45) which offers more insight. – John Mee Apr 29 '15 at 03:48
  • Thank you for your reply I had not seen that exception handling code. I've tried to implement it and use it with try: service.files().list().execute() except Exception as ex: str(ex) However I'm getting the following error message TypeError: the JSON object must be str, not 'bytes' I've looked for solutions for this on and found the following [Stackoverflow](http://stackoverflow.com/questions/6862770/python-3-let-json-object-accept-bytes-or-let-urlopen-output-strings) However the code is already using decode("utf-8") so i'm not sure what is going wrong :( – gowerc Apr 30 '15 at 21:10
  • Apparently the code was being run from \Lib\site-packages\googleapiclient\errors.py Did not have the .decode("utf-8") added. I have corrected this and extracted the HttpError which is . I am now looking into what this error actually means and how to fix it – gowerc Apr 30 '15 at 21:25
  • Found from another Stackoverflow thread that I needed to convert my service() call to service = build('drive', 'v2', http=http) – gowerc Apr 30 '15 at 21:34
  • Can you clarify how you fixed the initial problem? I'm hitting the same thing right now, first googleapiclient.errors.HttpError with the traceback to "TypeError; the JSON object must be str, not 'bytes'". Would appreciate your fix – sybaritic May 22 '15 at 09:57
  • For others troubled by the "unprintable HttpError": it's been fixed in the master branch of google-api-python-client, so just use that version until it's pushed to PyPi again and you'll be able to read the errors in Python 3, whatever they may be. – sybaritic May 22 '15 at 10:35
  • Hi @sybaritic , My fix was to manually edit the googleapi package to include .decode("utf-8"). For me the file was at **C:\Anaconda3\Lib\site-packages\googleapiclient\base.py** . The issue in question was line 47 = `data = json.loads(self.content)` Which I then changed to `data = json.loads(self.content.decode('utf-8'))`. Hope this helps – gowerc May 23 '15 at 11:24

1 Answers1

1

Issue was that

    service = build('drive', 'v2')

Should have been

    service = build('drive', 'v2', http=http)
gowerc
  • 1,039
  • 9
  • 18