【问题标题】:Accessing Apps for Work Gmail via Gmail API from personal account?从个人帐户通过 Gmail API 访问适用于工作的 Gmail 应用程序?
【发布时间】:2016-08-12 07:25:24
【问题描述】:

我已经在我的个人 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 帐户(个人或工作)都可以在明确授权后通过相同的代码访问。

【问题讨论】:

    标签: python gmail-api


    【解决方案1】:

    如果您的项目超出了每用户每秒 250 个配额单位的每用户速率限制,您可能会收到 403 的 HTTP 状态代码响应,如 Gmail API - Usage Limits 中给出的。

    Manage Delegation Settings 建议您的客户端应用程序在创建多个委托之间或在检索新创建的委托之间存在延迟,并且还指出每个委托 Gmail 访问权限的用户最多允许 25 个委托。您可以查看文档以获取有关 Gmail 委托的更多说明。

    【讨论】:

    • 感谢您尝试提供帮助,但您离题太远了。稍后我会发布我的问题的答案。
    【解决方案2】:

    所以答案(对于non-webapp Python)很简单,只要你找到它。

    这是来自 Python 快速入门应用的更新后的 get_credentials() 代码:

    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
    

    重要的变化是将 login_hint = 'a_new_account@gmail.com' 添加到 flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES, login_hint = 'a_new_account@gmail.com')

    这已记录在 here in the oauth2client documentation 中,虽然以典型的 Google 方式,但在 Python OAuth 2 API documentation 的任何地方都没有提及,我可以看到。

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 2012-07-26
      • 1970-01-01
      • 2016-09-28
      • 2015-08-27
      • 2022-01-15
      • 2011-04-09
      • 2016-01-08
      • 1970-01-01
      相关资源
      最近更新 更多