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 ?

7
  • 1
    An HttpError can contain a reason which offers more insight. Commented Apr 29, 2015 at 3: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 However the code is already using decode("utf-8") so i'm not sure what is going wrong :( Commented Apr 30, 2015 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 <HttpError 401 when requesting googleapis.com/drive/v2/files?alt=json returned "Login Required">. I am now looking into what this error actually means and how to fix it Commented Apr 30, 2015 at 21:25
  • Found from another Stackoverflow thread that I needed to convert my service() call to service = build('drive', 'v2', http=http) Commented Apr 30, 2015 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 Commented May 22, 2015 at 9:57

1 Answer 1

1

Issue was that

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

Should have been

    service = build('drive', 'v2', http=http)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.