in reply to google drive

I've shared an example here a few years back to interact with gmail. Wrote my own Authen\SASL\Perl\XOUTH2.pm to handle the Bearer. The Perl program was able to get the access token on the command line and it facilitates refreshing as well. I have not looked at your code in detail, but it looks similar, maybe the example is usefull to you: Send email with OAuth 2 through Gmail SMTP or API