I'm trying to get mail(imap) from google by oauth authorization . I have got the authorization to work, but I can not retrieve the emails. As I understand it should be posible. But Google does not have any api to retrieve mail(?).

I found the following:


That says:

Accessing mail using IMAP and sending mail using SMTP is often done using existing IMAP and SMTP libraries for convenience. As long as these libraries support the Simple Authentication and Security Layer (SASL), they should be compatible with the OAuth mechanism supported by Gmail. In addition to using a library which supports IMAP and SMTP, developers also will want to use one of the many existing libraries for handling OAuth

Do anyone know a existing library that i can use and that has some documentation as well? Im using google-api-php-client. Any thoughs or ideas are welcome! Thanks!

Below i the code that im using.

require_once '../../src/apiClient.php';

$client = new apiClient();
// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html
// Visit https://code.google.com/apis/console?api=contacts to generate your
// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.

if (isset($_GET['code'])) {
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

if (isset($_SESSION['token'])) {
  echo "token is set";

if (isset($_REQUEST['logout'])) {


if ($client->getAccessToken()) {

 MAGIC HAPPENS HERE!!!...but is unkown for me ofc

  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();
} else {
  $auth = $client->createAuthUrl();

if (isset($auth)) {
    print "<a class=login href='$auth'>Connect Me!</a>";
  } else {
    print "<a class=logout href='?logout'>Logout</a>";


There are no php mail apis that suports oauth 2.0 at the moment, unfortunatly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.