gpt4 book ai didi

python - 如何通过 Python 将 gpg 加密的 oauth 文件用于 offlineimap

转载 作者:太空狗 更新时间:2023-10-30 00:16:12 25 4
gpt4 key购买 nike

我正在研究 oauth2 以更好地理解它。出于这个原因,我安装了 offlineimap,它应该充当第三方应用程序。我在 stackexchange 上找到了一种读取加密凭证的好方法.

基于链接的帖子,我修改/复制了以下 python 脚本:

import subprocess
import os
import json

def passwd(file_name):
acct = os.path.basename(file_name)
path = "/PATHTOFILE/%s" % file_name
args = ["gpg", "--use-agent", "--quiet", "--batch", "-d", path]
try:
return subprocess.check_output(args).strip()
except subprocess.CalledProcessError:
return ""

def oauthpasswd(acct, key):
acct = os.path.basename(acct)
path = "/PATHTOFILE/%s_oauth2.gpg" % acct
args = ["gpg", "--use-agent", "--quiet", "--batch", "-d", path]
try:
return str(json.loads(subprocess.check_output(args).strip())['installed'][key])
except subprocess.CalledProcessError:
return ""

def prime_gpg_agent():
ret = False
i = 1
while not ret:
ret = (passwd("prime.gpg") == "prime")
if i > 2:
from offlineimap.ui import getglobalui
sys.stderr.write("Error reading in passwords. Terminating.\n")
getglobalui().terminate()
i += 1
return ret

prime_gpg_agent()

在相应的 offlineimaprc 文件中,我使用正确的参数调用该函数:

oauth2_client_id = oauthpasswd('gmail', 'client_id')
oauth2_client_secret = oauthpasswd('gmail', 'client_secret')
oauth2_request_url = https://accounts.google.com/o/oauth2/token
oauth2_refresh_token = passwd('gmail_rf_token.gpg')

请注意在本地文件中 PATHTOFILE 设置正确。我所做的是从谷歌下载 JSON 文件,包括 oauth2 凭证并对其进行加密。我已将刷新 token 存储在一个单独的文件中。但是,如果我运行 offlineimap,我会收到身份验证错误:

 ERROR: While attempting to sync account 'gmail'
('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x7f488c214320>) (configuration is: {'client_secret': "oauthpasswd('gmail', 'client_secret')", 'grant_type': 'refresh_token', 'refresh_token': "passwd('gmail_rf_token.gpg')", 'client_id': "oauthpasswd('gmail', 'client_id')"})

然后我尝试在 python 解释器中检查两个 python 函数 passwdoauthpasswd 的输出。我得到了想要的输出。更重要的是,我已经将 python 解释器中函数的输出复制到 offlineimaprc 配置文件中,并且能够同步到 Gmail。这意味着 offlineimap 执行文件时一定有错误,但我看不出有什么问题。

如果我只加密我的 Gmail 密码,一切正常。这意味着从 Google 下载的详细信息(client_id、client_secret 和刷新 token )出现了问题。如上所述,值(value)观本身是正确的。我真的复制了

的输出
oauthpasswd('gmail', 'client_id')
oauthpasswd('gmail', 'client_secret')
passwd('gmail_rf_token.gpg')

从 python 控制台到 offlineimaprc 文件,它起作用了。

最佳答案

发生的问题如下。根据这个answer offlineimap 不允许对 offlinemaprc 文件中的所有 key 进行加密。这就是为什么 python 函数永远不会被评估并且会传递错误的字符串。

关于python - 如何通过 Python 将 gpg 加密的 oauth 文件用于 offlineimap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46444477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com