0

I am following this example: https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/web-php

After the auth token expires, I get this error:

Fatal error: Uncaught Google_Service_Exception: { "error": { "code": 401, "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Request had invalid authentication credentials.", "reason": "authError" } ], "status": "UNAUTHENTICATED" } } in /Users/stephenbarrett/Reporting/src/Google/Http/REST.php:118 Stack trace: #0 /Users/stephenbarrett/Reporting/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 /Users/stephenbarrett/Reporting/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /Users/stephenbarrett/Reporting/src/Google/Http/REST.php(58): Google_Task_Runner->run() #3 /Users/steph in /Users/stephenbarrett/Reporting/src/Google/Http/REST.php on line 118

I went back to the basics and just used the same from the quickstart documentation and I got the same error after the Auth Token expired (1 hour). I'm using the PHP solution. And I noticed that there is an IF statement stating that the Auth Token doesn't exist get the oAuth2callback file, but it's not redirecting to the file at all. I have to manually enter in the oAuth2callback.php in the url and then I am able to renew my Auth Token. I'm just wondering why the IF statement is not working when using their sample code. Any help is greatly appreciated.

UPDATE I figured it out. I added another if statement to check the timestamp against the current timestamp to make it refresh the Auth Token

StephenB
  • 166
  • 2
  • 14

1 Answers1

0

1) Register your application in the Google Developers Console 2) Authorize access to Google Analytics data. 3) Create an Analytics service object

please refer the following url: Google Analytics API Returning 401 On Example Code