1

I am authenticating a Google Analytics account and retrieving data successfully. However, I want to run a cronjob to get data from the authenticated account once a week but the access token is invalid.

I have read a few things about refresh tokens but I'm unsure how to use them. What data should I be storing in my database to access the authenticated account whenever I like?

itsliamoco
  • 824
  • 7
  • 26
  • you need to store the refreshtoken, then you will be able to get a new access token when ever you need – DaImTo Aug 24 '14 at 12:16
  • Possible duplicate of [How to use refresh token to get authorized in background and getting access token?](http://stackoverflow.com/questions/33870439/how-to-use-refresh-token-to-get-authorized-in-background-and-getting-access-toke) – Prafulla Kumar Sahu Dec 17 '15 at 15:14
  • 1
    Possible duplicate of [How to refresh token with Google API client?](https://stackoverflow.com/questions/9241213/how-to-refresh-token-with-google-api-client) – Simon H Jul 20 '17 at 09:04

1 Answers1

0

To get your access token and refresh token, first create a file that contains the following code :

<?php

if (isset($_GET['code'])) {
    // try to get an access token
    $code = $_GET['code'];
    $url = 'https://accounts.google.com/o/oauth2/token';
    $params = array(
        "code" => $code,
        "client_id" => YOUR_CLIENT_ID,
        "client_secret" => YOUR_CLIENT_SECRET,
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "grant_type" => "authorization_code"
    );

    $ch = curl_init();
    curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url);
    curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);
    curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    if ($info['http_code'] === 200) {
        header('Content-Type: ' . $info['content_type']);
        return $output;
    } else {
        return 'An error happened';
    }
} else {

    $url = "https://accounts.google.com/o/oauth2/auth";

    $params = array(
        "response_type" => "code",
        "client_id" => YOUR_CLIENT_ID,
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "scope" => "https://www.googleapis.com/auth/plus.me"
    );

    $request_to = $url . '?' . http_build_query($params);

    header("Location: " . $request_to);
}

Then, replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your client ID and client secret.

Make sure your scope is correct. For example, it should be https://www.googleapis.com/auth/analytics if you want to get access to Analytics.

If you run this code, you should get an OAuth2 approval screen. Now, press Accept, and you should get a result that looks like this:

{
  "access_token" : YOUR_ACCESS_TOKEN,
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : YOUR_REFRESH_TOKEN
}

The result may contain additional fields, depending on which scope you're applying for.

John Slegers
  • 38,420
  • 17
  • 182
  • 152