Reputation: 27
I need an access token to make API calls of google analytics. which does not expire
already I created a curl API call using the google analytics query explore but the query explorer only gives the API access token which expires after 60 min.
curl_setopt($ch, CURLOPT_URL,'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A189254231&start-date=2019-02-01&end-date=2019-02-12&metrics=ga%3Ausers&dimensions=ga%3Asource&api=AIzaSyAUoXk0LxZeciEeMdxyOyvMVLOT6Phku4w');
The am getting the results properly but after 60 mins the token expires
Upvotes: 1
Views: 574
Reputation: 27
We can get a refresh token by developers.google.com/oauthplayground click on settings check Use your own OAuth credentials and enter OAuth Client ID and OAuth Client secret then select google analytics api in "select and authorize api" then we will get a refresh token.
Upvotes: 0
Reputation: 117301
Access tokens are designed to be short lived this way if they lost or stolen then will only work for a short time.
Refresh tokens
If you request offline access when authenticating your user you should be given a refresh token. This refresh token can be used to request a new access token when ever your access token has expired.
service account
If the account you will be accessing is your own account. Then you should consider using a service account. Service accounts are preapproved by adding the service account email address as a user in your google analytics account. By doing this it will always have access and you will not need to worry about it expiring.
I recommend using the Google apis php client library if you consider using service accounts.
Upvotes: 1