Created
December 2, 2012 05:01
-
-
Save mugifly/4187031 to your computer and use it in GitHub Desktop.
Perl - Gmail IMAP Using OAuth 2.0 (XOAUTH2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Perl - Gmail IMAP Using OAuth 2.0 (XOAUTH2) | |
# I referred to: http://eatenbyagrue.org/tags/oauth/index.html Thank you. | |
use strict; | |
use warnings; | |
use utf8; | |
use Mail::IMAPClient; | |
use URI::Escape; | |
use MIME::Base64; | |
my $oauth_token = "xxxxxxxxxxx"; # Get its token by using a Net::OAuth2 module, normally. | |
my $username = "[email protected]"; # Login ID of the user same as an above token. | |
my $oauth_sign = encode_base64("user=". $username ."\x01auth=Bearer ". $oauth_token ."\x01\x01", ''); | |
# detail: https://developers.google.com/google-apps/gmail/xoauth2_protocol | |
my $imap = Mail::IMAPClient->new( | |
Server => 'imap.gmail.com', | |
Port => 993, | |
Ssl => 1, | |
Uid => 1, | |
) or die('Can\'t connect to imap server.'); | |
$imap->authenticate('XOAUTH2', sub { return $oauth_sign }) or die("Auth error: ". $imap->LastError); | |
print $imap->folders or die("List folders error: ". $imap->LastError); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment