【发布时间】:2020-06-26 12:03:35
【问题描述】:
我在 Jetson Xavier 中有无头软件。我正在使用 Gmail API 发送邮件。我的代码在笔记本电脑中正常工作,但是在 Jetson 中禁用了 GUI 服务,无法打开浏览器。因此,当我尝试在 Jetson 中使用 Gmail API 时,身份验证失败。
我已将经过身份验证的“token.pickle”从笔记本电脑复制到 Jetson。它可以在短时间内正常工作。然后,它想要再次进行身份验证。
我该如何解决这个问题? Jetson中如何阻止浏览器打开进行身份验证?
谢谢。
这部分代码用于认证:
class EmailSender:
def __init__(self):
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.compose']
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
creds_dir = os.path.dirname(os.path.abspath(__file__)) + '/credentials.json'
flow = InstalledAppFlow.from_client_secrets_file(
creds_dir, SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
self.service = build('gmail', 'v1', credentials=creds)
【问题讨论】:
-
请编辑您的问题并包含您的代码
-
我已经编辑并包含了代码
-
浏览器总是需要打开,以便用户能够验证您的代码。请记住,InstalledAppFlow 用于已安装的应用程序,因此它将在运行它的机器上打开浏览器,而不是在作为 Web 应用程序运行时在用户机器上打开它。