0

This is driving me mental. I have a web application and an associated Google Account. I want the web application to use this Google drive and this google drive ONLY...PERMANENTLY.

I am using google/apiclient:^2.0 I have set up a OAuth 2.0 client ID and downloaded the JSON file.

I have this:

 $this->client = new \Google_Client();
 $this->client->setClientId('blahblahblah.apps.googleusercontent.com');
 $this->client->setAuthConfig(base_path() . '/resources/assets/client_secret.json');
 $this->client->setApplicationName('My Web App');
 $this->client->setRedirectUri('somewhere');
 $this->client->setScopes('https://www.googleapis.com/auth/drive');
 return $this->client;

Now when I run...

$authUrl = $this->client->createAuthUrl();
echo '<a href="'.$authUrl.'">Go</a>';

And authenticate I get a Code...

Now my question is...what do I do with that code?

I've tried...$this->client->authenticate('code here'); and also $accessToken = $client->fetchAccessTokenWithAuthCode('code here);

I keep getting either dailyLimitExceededUnreg or Invalid token format

I'm really confused and frustrated with the Google authentication API and the docs seem way out of date.

Any hints in the right direction would be amazing.

Thanks

Funktion
  • 539
  • 2
  • 5
  • 16

2 Answers2

0

To get the access token you need the following in you "somewhere" route:

$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();

the access token is used to log you into your google drive

to work with google drive you need to instantiate Google_Service_Drive

$drive = new Google_Service_Drive($client);
$files = $drive->files->listFiles(array())->getItems();

Note: access tokens are a form of user+password that expire over time so you need to fetch new ones if they expire

madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

I did something like this a couple of years ago and also had some difficulties with the documentation.

I went through the code to find it for you. I used this for Gmail contact list but the procedure looks the same. I'll try to explain the process I went through and I think it should help you out.

This it the part where you are. You get the code Google send you and just save it in a Session Variable

if (isset($_GET['code'])) {
    $auth_code = $_GET["code"];
    $_SESSION['google_code'] = $auth_code;
}

Now you will have to post to oauth2 to authenticate and get your acesstoken

$auth_code = $_SESSION['google_code'];
$max_results = 300;
$fields=array(
  'code'=>  urlencode($auth_code),
  'client_id'=>  urlencode($google_client_id),
  'client_secret'=>  urlencode($google_client_secret),
  'redirect_uri'=>  urlencode($google_redirect_uri),
  'grant_type'=>  urlencode('authorization_code')
);

$post = '';
foreach($fields as $key=>$value)
{
  $post .= $key.'='.$value.'&';
}

$post = rtrim($post,'&');
$result = curl('https://accounts.google.com/o/oauth2/token',$post);
$response =  json_decode($result);

$accesstoken = $response->access_token;

With the token you'll be able to curl Google Drive's endpoint and get your results

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse =  curl($url);
Carlos Alves Jorge
  • 1,919
  • 1
  • 13
  • 29