繁体   English   中英

yagmail 的 GMail Oauth2 身份验证在几天后停止工作

[英]GMail Oauth2 authentication for yagmail stops working after a few days

不久前,我开始使用 yagmail 使用 OAuth2 通过 GMail 发送邮件。

我使用的代码很简单:

    def send_with_yagmail(self):
        yag = yagmail.SMTP(self.our_email, oauth2_file="/<path_to>/credentials.json")
        yag.send(self.to_email, self.message_subject, contents=self.message_body)

几天后一切都很好,然后突然 Gmail 停止发送邮件,其中包含以下作为堆栈跟踪的一部分:

...
return get_oauth_string(user, oauth2_info)                            
File "/usr/local/lib/p│ython3.9/site-packages/yagmail/oauth2.py", line 96, in get_oauth_string
  access_token, expires_in = refresh_authorization(**oauth2_info)
File "/usr/local/lib/python3.9/site-packages/yagmail/oauth2.py", line 91, in refresh_authorization
  response = call_refresh_token(google_client_id, google_client_secret, google_refresh_token)
File "/usr/local/lib/python3.9/site-packages/yagmail/oauth2.py", line 71, in call_refresh_token
 response = urlopen(request_url, encoded_params).read().decode('UTF-8')
File "/usr/local/lib/python3.9/urllib/request.py", line 214, in urlopen 
  return opener.open(url, data, timeout)                                
File "/usr/local/lib/python3.9/urllib/request.py", line 523, in open    
  response = meth(req, response)                                        
File "/usr/local/lib/python3.9/urllib/request.py", line 632, in http_response
  response = self.parent.error(
File "/usr/local/lib/python3.9/urllib/request.py", line 561, in error   
  return self._call_chain(*args)
File "/usr/local/lib/python3.9/urllib/request.py", line 494, in _call_chain
  result = func(*args)│Ju
  File "/usr/local/lib/python3.9/urllib/request.py", line 641, in http_error_default
 raise HTTPError(req.full_url, code, msg, hdrs, fp)                    
urllib.error.HTTPError: HTTP Error 400: Bad Request                       

我通过在我的机器上本地重新生成credentials.json并认为这是一次性事件上传到服务器来解决它。 它工作了几天,现在又停止工作了。 所以我想知道:我应该怎么做才能不必每隔几天重新生成凭据? 我的 credentials.json 如下所示:

{
  "email_address": "my.team@gmail.com",
  "google_client_id": "74<blabla>0e1feov.apps.googleusercontent.com",
  "google_client_secret": "GOCSP<blabla<kOz",
  "google_refresh_token": "1//09<blabla>-2_dxZH8"
}

库使用的刷新令牌的目的不是让我不必手动重新生成这个东西吗?

任何人都可以推荐一些可以做的事情吗? 谢谢!

我遇到了同样的问题,您的应用程序是否使用 Google API 处于“测试”模式? 测试令牌的有效期为 7 天,这就是您必须重新生成令牌的原因。 我找到了这个答案,但是在我将状态更改为“生产中”之后,它现在给了我一个身份验证错误。 如果您使用的是 Gmail,则根据此页面,生产应用程序需要由 Google 验证,但我的凭据说它不需要验证,我没有看到请求验证的选项。 可能需要创建一个新的凭证?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM