Google API with PHP calling google_client object
<p>I'm using codeigniter 3.x and php 5.5+. I've been trying to get this to work with no luck. I want to call methods inside a youtube controller class I created that will do different things based on communicating with the google or youtube. The problem is with the Google_Client I believe or it's with how to store <code>$client</code> variable maybe so you can use it in second function. When I try calling this from another function it gives a not login required error even thought the user already did the google verification. How can I tell my second function that the user has already done the authorization and got the token. Everything seems to work very well when both functions are in the same function(I mean just using one function). Also I don't want to verify user on every function or method I execute.
So after first function is done, I get redirected with an access token in the url to the second function but then I get an error <code>Message: Error calling GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&amp;start-date=2014-01-01&amp;end-date=2016-01-20&amp;metrics=views&amp;filters=video%3D%3DpC900o6JaMc: (401) Login Required</code></p>

<p>First function in the controller Youtubeverify:</p>

<pre><code>class Youtubeverify extends CI_Controller{


public function youtube_consent(){


$this-&gt;load-&gt;library('google');

$OAUTH2_CLIENT_ID = 'MY_ID';
$OAUTH2_CLIENT_SECRET = 'MY_KEY';

$client = new Google_Client();
$client-&gt;setClientId($OAUTH2_CLIENT_ID);
$client-&gt;setClientSecret($OAUTH2_CLIENT_SECRET);

$client-&gt;setScopes(array('https://www.googleapis.com/auth/yt-analytics-monetary.readonly','https://www.googleapis.com/auth/youtube.readonly'));
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . "/ci/youtubeverify/display_report",
FILTER_SANITIZE_URL);
$client-&gt;setRedirectUri($redirect);

$youtubeReporting = new Google_Service_YouTubeReporting($client);
if (isset($_REQUEST['logout'])) {
$this-&gt;session-&gt;unset_userdata('youtube_token');
}

if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}

$client-&gt;authenticate($_GET['code']);
$youtube_token = $client-&gt;getAccessToken();
//trying to store the access token in a session
$newdata = array('youtube_token'=&gt;$youtube_token);

$this-&gt;session-&gt;set_userdata($newdata);

header('Location: ' . $redirect);
}

if ($this-&gt;session-&gt;userdata('youtube_token')) {
$client-&gt;setAccessToken($this-&gt;session-&gt;userdata('youtube_token'));
}

if ($client-&gt;getAccessToken()) {

$client-&gt;getAccessToken();
}
else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client-&gt;setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client-&gt;createAuthUrl();

 redirect($authUrl);
}
}
</code></pre>

<p>Second function in the controller Youtubeverify:</p>

<pre><code>public function display_report(){

$this-&gt;load-&gt;library('google');

$client = new Google_Client();

$client-&gt;getAccessToken();

$youtubeAnalytics = new Google_Service_YouTubeAnalytics($client);

$id = 'channel==MINE';
$start_date = '2014-01-01';
$end_date = '2016-01-20';
$optparams = array('filters' =&gt; 'video==*********');

$metrics = array('views');

$api_response = $metrics;


$api = $youtubeAnalytics-&gt;reports-&gt;query($id, $start_date, $end_date, $metric,$optparams);

$data['api_response'] = $api_response['views'];
$this-&gt;load-&gt;view('layouts/mainlay',$data);
}
</code></pre>