ImportError:无法导入名称GoogleCredentials

时间:2015-07-07 10:18:30

标签: python google-app-engine

我试图使用GoogleCredentials.get_application_default()

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)

我收到以下错误:

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

这种情况发生在开发和生产环境中。任何人都知道我做错了什么?

2 个答案:

答案 0 :(得分:2)

解决方案: 1. 卸载所有google-python工具     点击卸载googledatastore     pip uninstall google-api-python-client

来自添加/删除程序,卸载了谷歌应用引擎启动器

  1. 从程序文件和其他位置删除所有安装中的左侧文件。

  2. 重新安装google python工具 pip install google-api-python-client

  3. 现在错误消失了!

答案 1 :(得分:1)

在我的代码下面插入凭据

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))

appengine Google云端存储客户端库不支持设置或删除ACL条目。但SDK可以使用REST API和服务帐户来访问托管的云存储。 通过appengine服务帐户,可以非常轻松地使用OAuth2和Python API。 要在SDK中使用此功能,您必须在开发服务器中使用两个选项:

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