简体   繁体   中英

Fetch emails from Gmail via OAuth2

I'm trying to fetch emails from gmail using PHP and CodeIgniter, and an OAuth2 library. I have already got OAuth2 set up to get the users access token, etc. I can get the users info by doing

public function get_user_info(OAuth2_Token_Access $token)
{
    $url = 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&'.http_build_query(array(
        'access_token' => $token->access_token,
    ));

    $user = json_decode(file_get_contents($url), true);
    return array(
        'uid' => $user['id'],
        'nickname' => url_title($user['name'], '_', true),
        'name' => $user['name'],
        'first_name' => $user['given_name'],
        'last_name' => $user['family_name'],
        'email' => $user['email'],
        'location' => null,
        'image' => (isset($user['picture'])) ? $user['picture'] : null,
        'description' => null,
        'urls' => array(),
    );
}

What I want to do now is fetch some emails. I've googled for some code on how to get emails but the only thing I can see is https://mail.google.com/mail/feed/atom/ . I found this on the Google OAuth2 Playground but I can't figure out how to use it apart from navigating to it directly.

Can anyone give me some suggestions? Ideally I want to fetch emails that are not just new (this seems to be what the link above does).

Using a REST-based interface you can currently only get the unread messages feed . If you want to access all emails of a user, you have to use IMAP. Google developed a method to do IMAP/SMTP authentication using OAuth2 that you can use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM