ImportError: cannot import name GoogleCredentials
Question: I'm trying to use GoogleCredentials.get_application_default() in python in an
AppEngine project:

    
    
    from oauth2client.client import GoogleCredentials
    from ferris.core.google_api_helper import build
    
    ...
    
    gcs = build("storage", "v1", GoogleCredentials.get_application_default())
    response = gcs.objectAccessControls.insert(bucket=bucket,
                                               object=filename,
                                               body={"entity": "user-<email>",
                                                     "role": "READER", }).execute()
    logging.info(response)
    

And am receiving the following error:

    
    
    File "/base/data/home/apps/xxx", line 2, in <module>
        from oauth2client.client import GoogleCredentials
    ImportError: cannot import name GoogleCredentials
    

This happens both in the dev environment, and in production. Anyone have any
ideas what I'm doing wrong?

Thanks indeed,

Kiril.


Answer: Below my code to insert credentials

    
    
    import json
    from oauth2client import appengine
    from apiclient import discovery
    import httplib2
    import logging
    
    
    SCOPE_FULL_CONTROL = 'https://www.googleapis.com/auth/devstorage.full_control'
    
    http = httplib2.Http()
    credentials = appengine.AppAssertionCredentials(scope=SCOPE_FULL_CONTROL)
    http = credentials.authorize(http)
    client = discovery.build('storage', 'v1', http=http)
    
    
    def api_insert_gcs_user_acl(bucket, bucket_object, e_mail):
    
        # Cloud Storage API : https://developers.google.com/resources/api-libraries/documentation/storage/v1/python/latest/
        req = client.objectAccessControls().insert(
            bucket=bucket,
            object=bucket_object,
            body=dict(entity='user-' + e_mail, role='READER')
        )
    
        resp = req.execute()
        logging.info(json.dumps(resp, indent=2))
    

The appengine Google Cloud Storage Client Library does not support setting or
deleting ACL entries. But the SDK can use the REST API and a service account
to access the hosted Cloud storage. An appengine service accounts makes it
very easy to use OAuth2 and Python APIs. To make this work in the SDK, you
have to use two options in development server:

    
    
    --appidentity_email_address=<developer service account e-mail address>
    --appidentity_private_key_path=<d:/.../gcs-blobstore.pem key>
    

