我使用Google Application Default Credentials来使用Gmail API获取标签列表。Google应用默认凭据 - 开发中的“权限不足”和生产中的“请求不足”
当运行应用程序在本地使用gcloud preview app run
命令,我越来越HttpError: <HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/user%40domain.com/labels?alt=json returned "Insufficient Permission">
然后,我将应用程序部署和试图访问。但我得到HttpError: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/user%40domain.com/labels?alt=json returned "Bad Request">
我有正确的配置gcloud config list
:
account = [email protected] (Appengine Application Owener & Domain Admin)
disable_usage_reporting = False
project = <appengine_project_id>
gcloud版本的详细信息:
Google Cloud SDK 0.9.76
app 2015.08.27
app-engine-python 1.9.25
bq 2.0.18
bq-nix 2.0.18
core 2015.08.27
core-nix 2015.06.02
gcloud 2015.08.27
gsutil 4.14
gsutil-nix 4.12
preview 2015.08.27
而且我已经加入了服务帐户的客户ID & Gmail的适用范围:在谷歌企业应用套件https://www.googleapis.com/auth/gmail.readonly admin在Appengine控制台中启用CPanel和Gmail API。
这里是代码:
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials
class Connection:
def __init__(cls):
cls.credentials = GoogleCredentials.get_application_default()
def fetch_labels(cls):
service = build('gmail', 'v1', credentials=cls.credentials)
logging.info(service)
results = service.users().labels().list(userId='[email protected]').execute()
labels = results.get('labels', [])
if not labels:
logging.info('No labels found.')
else:
logging.info('Labels:')
for label in labels:
logging.info(label['name'])
pass
class Handler(webapp2.RequestHandler):
def get(self):
con = Connection()
con.fetch_labels()
创建服务帐户并下载JSON密钥。把它放在/home/{username}/.config/gcloud/application_default_credentials.json中。然后在重新启动后从devserver再次尝试。我认为部署的应用程序错误是不同的。 – ernestoalejo
@ernestoalejo我会尝试。如果有效,它只会在当地环境中解决这个问题。生产应用程序将有错误:( –
是的,它只是修复devserver。在生产中,有可能Google Apps没有正确配置,就像这个答案说的那样?http://stackoverflow.com/a/29328258/478440 – ernestoalejo