4

I am testing the Gmail API.

So far I have done the following:

So now when I run the file quickstart.php it gives a link. When I open it, it appears an authorization page where I authorize my application to access the Gmail API.

Then it redirects to the Redirect URIs that I have declared in the setup (adding the code parameter).

In the address bar it appears exactly this:

http://localhost/main/gmail_callback?code=MY_CODE

Where main is my controller and gmail_callback so far is just a blank function.

And it should be correct since these are my settings:

  • Javascript origins: http://localhost
  • Redirect URIs: http://localhost/main/gmail_callback

What do I do next?

FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178

3 Answers3

2

The next step in the flow is to exchange the Authorization Code for an Access Token (which will also include a Refresh Token if you requested offline access). If you use the https://developers.google.com/oauthplayground/ to execute the flow manually, you'll be able to see the URLs involved. There is a php library call to do the same thing, but I personally prefer to send my own HTTP rather than use a library. Even if you do use a library, it will still be worth spending a little time to understand the HTTP flow so you can more easily debug any problems you encounter.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Thank you, i understand better how it works. I have select and authorize Gmail API, exchange authorization code for the access tokens. I have then sent a request to get the messages and get a list of messages in json format. It seems to be fine. So if i understand properly what i need is to exchange the authorization code for the access tokens in my app too. That is what i miss. But how can that be done in my app? Thank you! – FrancescoMussi Jun 03 '15 at 08:45
  • Is this something you'll do for each user to access their own gmail, or is this a one off to access a central account? – pinoyyid Jun 04 '15 at 09:35
  • It is the one to access the central account i think – FrancescoMussi Jun 04 '15 at 09:57
  • If it's a one off exercise for a single central account, you can actually do the whole thing in the Oauth Playground without writing a single line of code. See http://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention-canonic?s=8|2.2789 – pinoyyid Jun 04 '15 at 10:15
1

Basically I was approaching wrongly. Following these instructions is enough to get the tokens:

https://developers.google.com/gmail/api/quickstart/php

The main point is to access the file from the command line and not from the app.

halfer
  • 19,824
  • 17
  • 99
  • 186
FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178
0

I made a Oauth Gmail some months ago, I got something like this :

In my callback function :

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  return $this->redirect($auth_url);
} else {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect_uri = Router::url('/', true).'Users/gmail';
  return $this->redirect($redirect_uri);
}

And in my gmail() function :

public function gmail(){ 
    require APPLIBS.'Google/src/Google'.DS.'autoload.php';
    $client = new Google_Client();
    $client->setAuthConfigFile('../Config/client_secrets.json');
    $client->addScope(Google_Service_Oauth2::PLUS_LOGIN);
    $client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
        $oauth_service = new Google_Service_Oauth2($client);
        $data['Profile']['last_name']  = $oauth_service->userinfo->get()->familyName; 
    } 
} 

$data['Profile']['last_name'] contain the last_name of the user, for example.

halfer
  • 19,824
  • 17
  • 99
  • 186
TiDJ
  • 433
  • 1
  • 4
  • 6
  • Thanks for reply. In the callback there is only that code? YOur gmail() function is where you send request to the API? (for instance if you want to get the messages?) – FrancescoMussi Jun 03 '15 at 08:07
  • Nop, there is this : public function oauthgmail2callback(){ // Callback require APPLIBS.'Google/src/Google'.DS.'autoload.php'; $client = new Google_Client(); $client->setAuthConfigFile('../Config/client_secrets.json'); $gmailredirecturi = Router::url('/', true).'Users/oauthgmail2callback'; $client->setRedirectUri($gmailredirecturi); $client->addScope(Google_Service_Oauth2::PLUS_LOGIN); $client->addScope(Google_Service_Oauth2::USERINFO_EMAIL); $client->setAccessType('online'); – TiDJ Jun 03 '15 at 12:56
  • Yes it is, for example the $data['Profile']['last_name'] value, etc. Depending on what you're looking for, depending of your $client->addScope / authorization – TiDJ Jun 03 '15 at 12:58