0

I have headless software in Jetson Xavier. I am using Gmail API for sending mail. My code is working correctly in Laptop however GUI service is disabled in Jetson, browser cannot be opened. So, Authentication is failed when I try to use Gmail API in Jetson.

I have copied authenticated "token.pickle" to Jetson from laptop. It works fine that for a short time. Then, it wants an authentication again.

How can I solve this issue? How to block the browser to be opened for authentication in Jetson?

Thanks.

This part of code is for authentication:

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)
  • Please edit your question and include your code – DaImTo Jun 26 '20 at 12:08
  • I have edited and included code – redrussianarmy Jun 26 '20 at 12:23
  • The browser will always need to open in order for the user to be able to authenticate your code. Just remember that InstalledAppFlow is used for installed applications so its going to open the browser on the machine that its running on as opposed to opening it on the users machine when running as a web application. – DaImTo Jun 26 '20 at 12:47

1 Answers1

-1

Unfortunately, gmail(and most modern auth service) will not allow you to login automatically directly for safety reason. In order to ensure that a third party service not to steal your account, google will ask you as a human to type in your account and password and google will return a cookie, or token, to the service you try to login.

However, since you are working on your own project and I assume you know the program will not put your account at risk, you can use selenium to simulate a browser and type your account automatically. You may refer to this post to learn how to do it.

whilrun
  • 1,482
  • 8
  • 20
  • The question is about the gmail api not gmail web application – DaImTo Jun 26 '20 at 12:14
  • Thank you for reply. I understand safety reasons. I accept that. My real point how to block the browser to be opened in Jetson. Because I have headless software – redrussianarmy Jun 26 '20 at 12:15
  • @redrussianarmy Oh I misundertand your question. As far as I know the official sdk does not give you a way to disable it. However you can use browser like https://www.brow.sh/ to login in headless environment. – whilrun Jun 26 '20 at 12:32
  • @whilrun Thanks for suggestion. I have installed browsh. However, it is not triggered automatically in my program and I can not see it in "www-browser" to make it default command line browser. How can I handle this? Do you have any idea? Thanks – redrussianarmy Jun 27 '20 at 08:15
  • @redrussianarmy You can manually add browsh in x-www-browser by using the following command `sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser $(which browsh) 150`. Make sure no other browser has priority higher than 150. Then your browsh should be the default browser to be open by other programs. – whilrun Jun 27 '20 at 08:36