user1195745
user1195745

Reputation: 65

fetch emails with php lib that suports oauth 2

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(?). However, I found the following:

https://developers.google.com/google-apps/gmail/oauth_overview

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.

The code

session_start();
ini_set('display_errors',1); 
error_reporting(-1);
require_once '../../src/apiClient.php';




$client = new apiClient();
$client->setApplicationName('Mailendar');
$client->setScopes("http://www.google.com/m8/feeds/");
// 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.
 $client->setClientId('secret');
 $client->setClientSecret('secret');
 $client->setRedirectUri('secret');
 $client->setDeveloperKey('secret');

if (isset($_GET['code'])) {
  $client->authenticate();
  $_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";
 $client->setAccessToken($_SESSION['token']);
}

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


  unset($_SESSION['token']);
  $client->revokeToken();
}

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>";


}

Thanks!

Upvotes: 0

Views: 1822

Answers (2)

Vivek Muthal
Vivek Muthal

Reputation: 1015

Now you can fetch mails using OAuth.
Implemented a simple library. Delete mail function is not yet added. But you can take a look if it satisfies your need.
Try example. https://github.com/vmuthal/VivOAuthIMAP

Upvotes: 0

user1195745
user1195745

Reputation: 65

Google doesnt allow you to retrieve mail with oauth 2.0 at the moment.

Upvotes: 1

Related Questions