0

我在 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)
4

1 回答 1

-1

不幸的是,出于安全原因,gmail(以及大多数现代身份验证服务)不允许您直接自动登录。为了确保第三方服务不会窃取您的帐户,谷歌会要求您作为人类输入您的帐户和密码,并且谷歌会向您尝试登录的服务返回一个 cookie 或令牌。

但是,由于您正在处理自己的项目,并且我假设您知道该程序不会使您的帐户面临风险,因此您可以使用 selenium 模拟浏览器并自动键入您的帐户。你可以参考这篇文章来学习如何做。

于 2020-06-26T12:10:02.777 回答