我已成功在我的个人Google帐户上试用Gmail API。为简单起见,consider the Python QuickStart example(工作正常)。问题是我无法使用相同的方法访问我的工作Gmail。
如果我只是将我的个人电子邮件地址替换为代码中的工作电子邮件地址......
results = service.users().labels().list(userId='myworkemail%40myworkdomain.com').execute()
...我得到标准委托拒绝错误:
<HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/myworkemail%40myworkdomain.com/labels?alt=json returned "Delegation denied for mypersonalemail@gmail.com">
根据之前StackExchange问题的一些提示,我尝试通过说明制作service account and an authorised API call,将服务帐户作为我个人帐户的开发者控制台...
credentials = ServiceAccountCredentials.from_json_keyfile_name('service_account.json', scopes=SCOPES)
delegated_credentials = credentials.create_delegated('myworkemail%40myworkdomain.com')
http_auth = delegated_credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http_auth)
results = service.users().labels().list(userId='myworkemail%40myworkdomain.com').execute()
...但是却得到了不同的错误:
File "qs.py", line 70, in main
results = service.users().labels().list(userId='myworkemail%40myworkdomain.com').execute()
File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 135, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/googleapiclient/http.py", line 755, in execute
method=str(self.method), body=self.body, headers=self.headers)
File "/usr/local/lib/python2.7/site-packages/googleapiclient/http.py", line 93, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 597, in new_request
self._refresh(request_orig)
File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 863, in _refresh
self._do_refresh_request(http_request)
File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 932, in _do_refresh_request
raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
oauth2client.client.HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request.
另外需要注意的一点是:Gmail API参考页上的the API explorer examples可以正常使用我的工作帐户,并在“我的帐户”中显示为已授权。我想要的只是相同的行为:对于任何 Gmail帐户(个人或工作),可以通过相同的代码访问,并遵循明确的授权。
答案 0 :(得分:2)
如果Gmail API - Usage Limits中提供的项目超出了每个用户每秒每用户250个配额单位的每个用户速率限制,则可能会收到403的HTTP状态代码响应。
建议在Manage Delegation Settings中,您的客户端应用程序在创建多个代理之间或在检索新创建的代理之间存在延迟,并且还指出每个用户委派Gmail访问权限最多允许25个代表。您可以查看文档以获取有关Gmail委派的更多说明。
答案 1 :(得分:0)
一旦你找到了答案(对于非webapp Python)很容易。
这是Python快速入门应用程序中更新的android.intent.action.BOOT_COMPLETED
代码:
get_credentials()
重要的变化是将 login_hint =&#39; a_new_account@gmail.com' 添加到def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'gmail-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES, login_hint = 'a_new_account@gmail.com')
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
这是记录here in the oauth2client documentation,虽然以典型的谷歌方式,我在Python OAuth 2 API documentation的任何地方都没有提及,我可以看到。