0
votes

I'm testing the Google Analytics API (with oauth 2.0) on my local machine and I want to know if it's possible to get it to work this way as they request me to insert a Redirect URI in the Google APIs Console and then enter it in my code but I do not know what this Redirect URI should be?

My current Redirect URI is https://localhost/oauth2callback and I tried https://gapi.local/oauth2callback but neither works for me.

I get this error message:
Fatal error: Call to undefined method apiClient::setClientRedirectUri() in C:\xampp\htdocs\webs\gapi\HelloAnalyticsApi.php on line 15

Any help would be appreciated.

1
When does that error happen? Before redirecting? After you were redirected back? If the browser was redirected back, does it actually run any line of your code? - Jan Gerlinger
The problem is that no redirection occurs. When I run the file, it's supposed to display the amount of visitors to the site of my GA account but it just displays the error message above. I used the exact same code as in this tutorial: developers.google.com/analytics/resources/tutorials/… - Zamalekk

1 Answers

1
votes

The google-api-php-client library has no method setClientRedirectUri() in apiClient. The correct method is called setRedirectUri():

$client = new apiClient();
$client->setApplicationName('Hello Analytics API Sample');

// Visit //code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_developer_key');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));