Google Analytics PHP Interface (GAPI) OAuth 2.0 token expiring - php

I'm currently using the GAPI tool to extract date from my Google Analytics account using an OAuth 2.0 account created in the Google Developers Console. It works flawlessly except for one thing - every so often, the script will fail with the following message:
'GAPI: Failed to request report data. Error: "{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}"
My assumption is that the Oauth 2.0 token is expiring, preventing further data access. I have been manually restarting the process so far, but it would be far more efficient to automatically refresh the token prior to expiration. If anyone knows the best way to accomplish this, I'd appreciate hearing it. Thanks.

Related

GMB PHP API 2020 Issues

I am working on the GMB API, facing some issues. I have implemented Google Oauth at frond-end and saving user listing in my DB. Now I want to run cron job in which I want to get those user`s listing insights. So issues are here
1: I can't find GMB API samples or doc regarding GMB stuff(I have read all google-api-php-client doc).
Here are GMB samples but it doest have PHP samples.
https://github.com/google/google-my-business-samples
2: Second thing is that I don't want to open consent(Google Account Selector Popup) to get the user's permission because its a backend process. So what type of process should I follow a) Oauth OR b)Service Account.
If I use the Oauth process then I have to save access_token in the database when the user first time gives permission at the front end of the website and uses that token as refresh token to get the new access token in PHP and call GMB APIs.
But the issue is that we have already registered 500+ users, so how I will get their access token because they are already done with google permissions.
I didn`t try yet with a service account.
I am using Laravel 5.7 version.

Delete from my YouTube Channel Programmatically

Previously I had a working php script that uses the Zend_Gdata_ClientLogin Object to authenticate and make requests on my behalf to delete videos from my YouTube channel. Apparently, this has been deprecated because I get an error and read this: zend gdata and google spreadsheet not connecting
And I get an error which says no longer available. So I've been researching how to use YouTube API v3. Initially I created a service account via the Google developer console.
However I found this
YouTube API v3 with OAuth2: update and delete fail with "Insufficient Permission" error
Which states:
For the uninitiated, the YouTube Data API (v3) does not support
"service accounts,"
And I need to manually obtain a refresh token, to make requests?
Is this still the case? Surely, there must be an easier way to make authenticated requests to the YouTube API where the only client is my server?
If this is still true, how long will the refresh token work? Is this the best practice?
You can check out the documentation to see the most up to date way to request the access token. When the access token expires, your application can refresh it by sending a POST request to Google's authorization server using the refresh token. You only need to get this refresh token once in order to refresh your access token, not every time you want to make authenticated requests, if that's what you were thinking.
The refresh token should be valid unless:
The user has revoked access.
The token has not been used for six months.
The user account has exceeded a certain number of token requests.
There is currently a 25-token limit per Google user account, so if your application requests too many refresh tokens, it may run into this limit and older refresh tokens will stop working.
I can't speak as to whether this is the best way of doing it, but I assume the developers chose this way for a reason.

Google Adwords API "An error has occurred: { "error" : "invalid_grant" }"

I've been crawling the internet all morning trying to figure this out. I'm trying to get access to a Adwords test account through the API but I keep receiving "An error has occurred: { "error" : "invalid_grant" }". I've seen this is quite a common problem so perhaps if I outline what I've done step by step someone can correct me. Hopefully!
Created a Google adwords account and set up a campaign.
Created a MCC account. Added Google adwords account and applied for API access (pending). Noted API key, client id for auth.ini.
Created a test MCC account and added Google adwords account. Noted client id for auth.ini.
Using the Google account of both MCC accounts went into the API console and created a new installed application app. Noted all keys/tokens.
Downloaded the PHP code examples and libs. Ran get GetRefreshToken.php, granted accesses to app(s) and noted refresh token for auth.ini
Set up auth.ini populating developerToken, userAgent, clientCustomerId, client_id, client_secret and refresh_token.
Now I've tried using BOTH sets of tokens and ids from the test MCC account API console and the live MCC account API console. Both give me the same error. I've tried regenerating all tokens several times.
Gave up and came crawling to StackOverFlow.
Is the invalid_grant message related to Auth or my developerToken token not being activated yet (sure it said I could use my test account though) or because I haven't set up any of the billing?
In my case the problem was at step 5 in your list; I had copied the authorization code from my browser after granting access, rather than the refresh token generated by GetRefreshToken.php after I pasted the authorization code there.
I had the same situation for a long time.
But solution was very simple.
The code getting after authorizing the Google is not your refresh token.Its is only an access token(I guess.)
You have to add this code to GetAccessToken's $code variable function in getrefreshtoken.php file and it will return the actual Refresh token.
This solved my issue.
It is very important to note that the SDK is designed to be used through PHP-Cli. So please don't use it via browser. If you still run it by commenting out return line you'll get into further problems to comment that on each file :)

Google Analytics API got me completely lost

Until recently we have been using the old gapi php class to extract Google Analytics data from a variety of sites that we manage.
In a nutshell at night the sites download the Analytics data and stores it locally. Until recently it worked beautifully but all of a sudden we started receiving all sorts of weird errors like CaptchaRequired.
Anyway I've done some reading and got the impression that it was time we move to the new Google APIs platform and while I have tried to follow the HelloAnalyticsAPI tutorial we initially got a 403 error but now after leaving it for a while when I try and run the app I get redirected to Google to login.
I just don't seem to be able to get my head around it. We need to completely automate this process so redirecting to login on the Google site isn't going to cut it.
Can anyone help? Anyone seen these issues?
Use google api client for php, it's easy
http://code.google.com/p/google-api-php-client/
And dev guide for analytics (V3):
https://developers.google.com/analytics/devguides/config/mgmt/v3/
Google sends Captcha responses if they are unsure about the authenticity of the request. This can be caused by multiple requests from the same IP for various different accounts, use of the ClientLogin authentication mechanism, and even weak passwords on the account you are logging in to.
To prevent them, I would recommend using OAuth 2.0 for the authentication - there are other security benefits to using this too.

Adwords API Bad Authentication

I am using Adwords API from last 3 months and all of a sudden today it is coming up with error message as follows:
Uncaught exception 'AuthTokenException' with message 'Failed to get authToken. Reason: BadAuthentication'
I am using google adwords V201101 php library with auth.ini containing all user details and settings.ini with https://adwords.google.com not sandbox environment. Can anyone help me with this please???
Thanks,
Murali.
you may requesting authToken from ClientLogin service too many times, that is way you get 'CaptchaRequired'.
Request authToken just for the first time, and then cache it for subsequent requests.
Check this out http://goo.gl/TOX6N
I recently had this problem trying to connect to Google DFP. I copied perfectly working code from my localhost after development and ran it on my remote cloud server. I then received the
Failed to get authToken
message. The reason was that google was blocking the attempted login as a security measure because it was a new location. I had to log into the google account (from the web) with those credentials in order to see the message and confirm the added login location. Everything worked fine after that authorization.
I've stumbled across this question and thought that this info may help someone else.
BadAuthentication means your username/password is incorrect. May be someone changed the account password, may be auth.ini got modified by mistake.
See detailed error codes here: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Errors
Cheers,
Anash
I started getting this error after somebody in my organization updated the password for the account my app was using to log in. The best way to avoid this happening again is to use OAuth2.
It's as easy as going to the Google API console and creating a Client ID for Installed Applications (under API access) for your project. You then use the Client ID and Client Secret in your Adwords API auth.ini file.
Next you run the examples/AdWords/Auth/GetRefreshToken.php script which will have you grant your app access to your Adwords account. You'll end up with a Refresh Token which you need to add to your auth.ini.
The ClientLogin (username and password) method of authentication is being deprecated in favour of the above so best to migrate ASAP.

Categories